-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Type Guards do not reflect on related types #25040
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
|
You can use function overloads (if you want that the two parameters has the same type): type Identity<T> = T;
function test(x: 'a', y: Identity<typeof x>);
function test(x: 'b', y: Identity<typeof x>);
function test(
x: 'a' | 'b',
y: Identity<typeof x>,
) {
if (x === 'a') {
const z = y;
}
} |
@j-oliveras But that won't fix his issue that |
I'm trying to understand if there is a way to constrain a function arguments with some relation, i.e. having the same type. For example, what if I want a function that takes two arguments of the same type: f<T>(a: T, b: T) Within the implementation, narrowing does not work with generics at all #25039. So we need to write it without generics, but it seems that there is no way to express this relation without using generics? |
This would require dependent types which is not something that TypeScript offers. |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
TypeScript Version: 2.8
Code
Expected behavior:
x
has typea
z
has typea
Actual behavior:
x
has typea
z
has typea | b
The text was updated successfully, but these errors were encountered: