Last active
July 6, 2023 09:56
-
-
Save HikaruEgashira/a223332d3e4189b32bde32e641238e9b to your computer and use it in GitHub Desktop.
C言語などによくあるprintfにTypeScriptで型をつける
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Parser<T> = | |
T extends `%s${infer U}`? [string, ...Parser<U>] | |
: T extends `%d${infer U}` ? [number, ...Parser<U>] | |
: T extends `${string}${infer U}` ? Parser<U> | |
: []; | |
type Format = <T extends string>(str: T, ...params: Parser<T>) => string | |
const format: Format = (str, ...params) => str // WIP | |
format("$%d/%s", 5, "day") // $5/day | |
// ^? const format: <"$%d/%s">(str: "$%d/%s", params_0: number, params_1: string) => string | |
// playground-> https://www.typescriptlang.org/play?jsx=0&pretty=true#code/PTAEGFAAow8qMOwYAcBOBLAdgFwDasNYMho9UCSGQEwZBFBkGiGAKApFAFIBnAWgD5R70VUBzasWgExahUAVwC2AIwCmiKugCe8KaAAKAQ0T0ZAHgAqrALwVQJ07tBSAHuimp+9UAAMGAEgDeaAGYzQAVQC+jgD8oADa7JxcADSgAHTx6po6vswAusamAFyg5lY2dg7O-O5ePgGOoCGhopIyMfGxiVqI2inppqDZuda29k7uEWhc-iWo3oh+gZWqGs2tzBkm2aGpANwUCkqgAGIA9ohiauigBqB6Fj0FbBxDzAAUEV318fAaamL02U06+gCUJ6xBtwqABjXaodigTz7Q7obJ7A5HE6gB4cZ6xV6Id70f4GQEcUA0ADqAEkVBRoYj0HcAEQuATABg0mIAVhiNP4ankNP+NBcLOAnPkvFAAD0ghQgA |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment