Last active
May 10, 2022 14:22
-
-
Save jfet97/f2dd5732f3acac8898e7c29059fc0d68 to your computer and use it in GitHub Desktop.
Il tipo di una stringa lunga almeno 5
This file contains hidden or 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
// 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