-
-
Notifications
You must be signed in to change notification settings - Fork 542
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Not<string, 'not me'>
#417
Comments
I think it could be useful. If only TS had negated types: microsoft/TypeScript#29317 |
I can't make it work as a reusable type: type Not<Yes, Not> = Yes extends Not ? never : Yes;
type NotHello = Not<string, 'hello'>;
const notHello: NotHello = 'hello';
// No error because `NotHello === string` It's possible that TypeScript "forgets" the negation too early The generic-based works but it's buggy with variables: type Not<Yes, Not> = Yes extends Not ? never : Yes;
type NotHello<S extends string> = Not<S, 'hello'>;
export const join = <S extends string>(
...parts: Array<NotHello<S> | number>
): string => parts.join(',');
join('hello'); // Error 🎉
join('hello', 'world'); // Error 🎉
let nonConstString = 'sup'
join('hello', nonConstString); // No error 😰 |
They needs to be Checkout the following code on TS Playground: type Not<Yes, Not> = Yes extends Not ? never : Yes;
type NotHello<S extends string> = Not<S, 'hello'>;
export const join = <S extends string>(
...parts: Array<NotHello<S> | number>
): string => parts.join(',');
join('hello'); // Error 🎉
join('hello', 'world'); // Error 🎉
let nonConstString = 'sup' as const
join('hello', nonConstString); // Error 🎉 |
Could microsoft/TypeScript#51865 help solve this issue? |
I was looking for a way to implement “any string that doesn’t start with a slash” with template literals, then I realized that it can be achieved with a generic
Not
type:Is there anything like
Not
?Edit: related:
Upvote & Fund
The text was updated successfully, but these errors were encountered: