We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
Not
NotIn
NoIntersect
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
There are several goals I am hoping to achieve here, with these type helpers:
Ensure a type is not another type
const a: string & Not<"test"> = "test"; // ^ TS should complain
Ensure a type is not in another grouped type like an array or object
Array example
const forbiddenStrings = ['bad', 'worse', 'worst'] as const; const a: string & NotIn<typeof forbiddenStrings> = "bad"; // ^ TS should complain
Object keys example
const forbiddenStrings = { bad: 1, worse: 2, worst: 3 } as const; const a: string & NotIn<keyof typeof forbiddenStrings> = "bad"; // ^ TS should complain
Compare objects of different types to ensure none contain the same keys or elements (in the case of arrays)
const wordsA: string[] & NoIntersect<typeof wordsB, keyof typeof wordsC> = ['a', 'b', 'c'] as const; // ^ TS should complain const wordsB: string[] & NoIntersect<typeof wordsA, keyof typeof wordsC> = ['d', 'e', 'f'] as const; // ^ TS should complain const wordsC: string[] & NoIntersect<typeof wordsA, typeof wordsB> = { g: 1, h: 2, i: 3 } as const; // ^ TS should complain
I'm' not sure what the API for this would look like, likely not what I showed above but something more argument-based like this…
type SomeType = NoIntersect<typeof wordsB, keyof typeof wordsC>;
…if the base type can be inferred, otherwise passed explicitly as the first arg like this:
type SomeType = NoIntersect<string[], typeof wordsB, keyof typeof wordsC>;
The text was updated successfully, but these errors were encountered:
This is not possible in typescript. Feature request was rejected long time ago from TS team : microsoft/TypeScript#47178
Sorry, something went wrong.
No branches or pull requests
Uh oh!
There was an error while loading. Please reload this page.
There are several goals I am hoping to achieve here, with these type helpers:
Ensure a type is not another type
Ensure a type is not in another grouped type like an array or object
Array example
Object keys example
Compare objects of different types to ensure none contain the same keys or elements (in the case of arrays)
I'm' not sure what the API for this would look like, likely not what I showed above but something more argument-based like this…
…if the base type can be inferred, otherwise passed explicitly as the first arg like this:
The text was updated successfully, but these errors were encountered: