FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Improve type guards for type variables by ahejlsberg · Pull Request #15576 · microsoft/TypeScript · GitHub

Improve type guards for type variables - #15576

Merged
Anders Hejlsberg (ahejlsberg) merged 5 commits into
masterfrom
typeVariableTypeGuards
May 7, 2017
Merged

Improve type guards for type variables#15576
Anders Hejlsberg (ahejlsberg) merged 5 commits into
masterfrom
typeVariableTypeGuards

Conversation

Copy link
Copy Markdown
Member

With this PR, when a node is the left hand expression of a property access, element access, or call expression, and the type of the node includes type variables with constraints that are nullable, we fetch the apparent type of the node before performing control flow analysis such that narrowings apply to the constraint type. For example:

type Item = {
    (): string;
    x: string;
}

function f1<T extends Item | undefined>(obj: T) {
    if (obj) {
        obj.x;     // Ok
        obj["x"];  // Ok
        obj();     // Ok
    }
}

Prior to this PR, the property access, element access, and call expressions above were errors.

Fixes #14091.
Fixes #14415.

Comment thread src/compiler/checker.ts

function isApparentTypePosition(node: Node) {
const parent = node.parent;
return parent.kind === SyntaxKind.PropertyAccessExpression ||

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

&& (<PropertyAccessExpression >parent).expression === node; ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

No need to. This function is only ever called on the left hand side of a property access expression (the right hand side is just an identifier and not actually an expression).

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants


Back | FazBrowse Home | New Git URL