Check inferred constraints for 'infer X' type variables#22323
Merged
ahejlsberg merged 4 commits intomasterfrom Mar 5, 2018
Merged
Check inferred constraints for 'infer X' type variables#22323ahejlsberg merged 4 commits intomasterfrom
ahejlsberg merged 4 commits intomasterfrom
Conversation
mhegazy
approved these changes
Mar 5, 2018
|
@ahejlsberg is there a way to express type Not<T, U> = ... // T can be anything but U, if T is U then error
// use case:
function notSupposedToTakeAsyncCallbacks<X>(callback: () => Not<X, Promise<any>>) {
} |
Contributor
|
there is not really a way to throw an error. so the best that is possible today is to return a type that will cause a down-stream error, something like type Not<T, U> = T extends U ? never : T;
type t1 = Not<{ a: number }, Promise<any>>; // { a: number }
type t2 = Not<Promise<number>, Promise<any>>; // never |
|
another silly questions please |
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
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
With this PR we properly check inferred constrains for
infer Xtype variables in conditional types. Specifically, during type inference in a conditional type, when a candidate type for aninfer Xtype variable doesn't satisfy the constraint inferred for that type variable (see #21709), the candidate is replaced with the constraint. That will subsequently cause the conditional check to be false. For example:Previously,
T2would be the type42which doesn't satisfy the constraintstringinferred fromMustBeString.Fixes #22221.