Skip to content
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

Support direct typing when using instanceof for better intellisense #6518

Closed
Alphapage opened this issue Jan 17, 2016 · 2 comments
Closed

Support direct typing when using instanceof for better intellisense #6518

Alphapage opened this issue Jan 17, 2016 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@Alphapage
Copy link

Hello,

Here is my code:

let value: any
 if (value instanceof MyClass) {
     // value is any. needs to typecast like this to get intellisense working:
     (<MyClass>value).prop=true;
}

It would be great to support this feature.

Thanks in advance and I hope this could help others.

@Arnavion
Copy link
Contributor

instanceof already narrows types.

let value2: MyClass | string;
if (value2 instanceof MyClass) {
    value2.prop = true; // value2 is a MyClass inside the if-block. No typecast needed.
}

The reason it doesn't in your example is specifically because value is of any type, which instanceof doesn't narrow. See #1426 for why it works this way.

@DanielRosenwasser DanielRosenwasser added By Design Deprecated - use "Working as Intended" or "Design Limitation" instead Duplicate An existing issue was already created labels Jan 18, 2016
@DanielRosenwasser
Copy link
Member

Duplicate of #1595, #1938, and #6301.

@RyanCavanaugh RyanCavanaugh removed the By Design Deprecated - use "Working as Intended" or "Design Limitation" instead label Jan 19, 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
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants