Skip to content

Type narrowing order of generics and non-generics #6607

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

Closed
falsandtru opened this issue Jan 25, 2016 · 1 comment
Closed

Type narrowing order of generics and non-generics #6607

falsandtru opened this issue Jan 25, 2016 · 1 comment
Labels
External Relates to another program, environment, or user action which we cannot control.

Comments

@falsandtru
Copy link
Contributor

Currently, instance operator narrowing order is left to right and non-generics to generics, but should not have order of non-generics to generics. Narrowing order should only depend definition order in union types.

class A<T> {
    prop: T;
}
class B<T> extends A<T> {
}
class C extends A<any> {
}

var ab: A<string> | B<string>;
if (ab instanceof A) {
    ab; // A<string>
}
var ba: B<string> | A<string>;
if (ba instanceof A) {
    ba; // B<string>
}
var ca: C | A<string>;
if (ca instanceof A) {
    ca; // C
}
var ac: A<string> | C;
if (ac instanceof A) {
    ac; // C, should be A<string>
}

Note: TypeScript should provide the way that select the most closest type by definition order in union types for alternate method of pattern matching.

derived from #6589

@falsandtru falsandtru changed the title Type narrowing order between generics and non-generics Type narrowing order of generics and non-generics Jan 25, 2016
@falsandtru
Copy link
Contributor Author

This order may depend abstraction level, not generics or non-generics.

@RyanCavanaugh RyanCavanaugh added the External Relates to another program, environment, or user action which we cannot control. label Jan 25, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
External Relates to another program, environment, or user action which we cannot control.
Projects
None yet
Development

No branches or pull requests

2 participants