I know we can use Omit<>
to type a certain object without specific props.
I was hoping we could also use this for string literals:
type possibleStrings = 'A' | 'B' | 'C'
type AorB = Omit<possibleStrings, 'C'>
But when trying to use something like this in a function for its params, I get this error:
Type 'Pick' cannot be used as an index type.
String literal syntax Use the escape sequence \n to represent a new-line character as part of the string. Use the escape sequence \\ to represent a backslash character as part of the string. You can represent a single quotation mark symbol either by itself or with the escape sequence \' .
Hence, you can treat a variable that has a string literal type like a variable of type string . You can access properties, call methods, and use operators, just as you would with regular strings: const eventName: "click" | "mouseover" = "click"; eventName. length; // 5 eventName.
The string literal type allows you to specify a set of possible string values for a variable, only those string values can be assigned to a variable. TypeScript throws a compile-time error if one tries to assign a value to the variable that isn't defined by the string literal type.
A "string literal" is a sequence of characters from the source character set enclosed in double quotation marks (" "). String literals are used to represent a sequence of characters which, taken together, form a null-terminated string. You must always prefix wide-string literals with the letter L.
You can use Exclude
for omitting a single string in a String Literal.
type MyStringLiteral = 'A' | 'B' | 'C'
type AorB = Exclude<MyStringLiteral, 'C'>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With