Skip to content

Instantly share code, notes, and snippets.

@jfet97
Last active May 10, 2022 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jfet97/f2dd5732f3acac8898e7c29059fc0d68 to your computer and use it in GitHub Desktop.
Save jfet97/f2dd5732f3acac8898e7c29059fc0d68 to your computer and use it in GitHub Desktop.
Il tipo di una stringa lunga almeno 5
// 6 any perché l'ultimo "viene inferito" come la stringa vuota "", gli altri 5 acchiappano un carattere ciascuno
type String5<T extends string> = T extends `${any}${any}${any}${any}${any}${any}` ? T : "The string must be at least 5 characters long" // oppure never
type testmeno1 = String5<string> // nope
type test0 = String5<""> // nope
type test1 = String5<"a"> // nope
type test2 = String5<"ab"> // nope
type test3 = String5<"abc"> // nope
type test4 = String5<"abcd"> // nope
type test5 = String5<"abcde"> // "abcde"
type test6 = String5<"abcdef"> // "abcdef"
declare function foo<S extends string>(s: String5<S>): any;
foo("ciao") // errore: S = "ciao", typeof s = "The string must be at least 5 characters long"
foo("come stai?") // ok
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment