Skip to content

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

Closed
@zhuravlikjb

Description

@zhuravlikjb

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?

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issueSpecIssues related to the TypeScript language specification

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions