-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Constraints applied by conditional types not considered when checking signatures #22347
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
Comments
This is actually working as intended. The constraint added to type EQString<T> = T extends string ? T & string : never; |
Hm. OK. That's not the greatest in the context of using conditionals for control flow narrowing, since it causes you to get intersections instead of constrained generics like you'd expect. |
@weswigham You very well might already know this, but another way to get your example to work without the intersection is declare function log(msg: string): void
type MustBeString<T extends string> = T
type EQString<T> = T extends MustBeString<infer U> ? U : never
function c<T>(x: T | string) {
const y = x as EQString<T> // assert stringiness
return log(y)
} |
It's a little verbose but it seems like it checks out. |
Erm, doesn't check out too well, in heavy use practice. It may give the result a new constraint on whatever you say, but it forgets its relationship with the original declare function log(msg: string): void
type MustBeString<T extends string> = T;
type EQString<T> = T extends MustBeString<infer U> ? U : never
function c<T>(x: T | string) {
const y = x as EQString<T> // assert stringiness
return useStringyT(y); /// Noooooooo: Argument of type 'EQString<T>' is not assignable to parameter of type 'T & string'
function useStringyT(x: T & string) {
return 0;
}
} |
sigh just intersecting doesn't quite work either, since, for example, in concrete types Replacing the constraint via an |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
Code
Expected behavior:
No error.
Actual behavior:
on
y
.The text was updated successfully, but these errors were encountered: