Skip to content

Spec contradicts to itself in one of the examples with type guards #1808

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
zhuravlikjb opened this issue Jan 26, 2015 · 1 comment · Fixed by #1878
Closed

Spec contradicts to itself in one of the examples with type guards #1808

zhuravlikjb opened this issue Jan 26, 2015 · 1 comment · Fixed by #1878
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue Spec Issues related to the TypeScript language specification

Comments

@zhuravlikjb
Copy link

In the section about type guards, the last example seems to be invalid:

class NamedItem {  
    name: string;  
}

function getName(obj: any) {  
    return obj instanceof NamedItem ? obj.name : "unknown";  
}

It is stated that the inferred type of the 'getName' function is string, but actually 'any' type won't be narrowed, according to the same spec, and compiler doesn't do that either:

getname

A bug in the spec?

@ahejlsberg
Copy link
Member

Yes, that is a bug in the spec.

@ahejlsberg ahejlsberg added Bug A bug in TypeScript Spec Issues related to the TypeScript language specification labels Jan 26, 2015
@ahejlsberg ahejlsberg added this to the TypeScript 1.5 milestone Jan 26, 2015
@ahejlsberg ahejlsberg self-assigned this Jan 26, 2015
@ahejlsberg ahejlsberg added the Fixed A PR has been merged for this issue label Feb 2, 2015
@microsoft microsoft locked and limited conversation to collaborators Jun 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue Spec Issues related to the TypeScript language specification
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants