-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Improve type guards for type variables #15576
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3448164
Obtain apparent type before narrowing type variables
ahejlsberg 3d069f7
New behavior only for type variables with nullable constraints
ahejlsberg 238067e
Add tests
ahejlsberg 4123068
Only get apparent type when constraint includes nullable types
ahejlsberg a6dfd66
Update tests
ahejlsberg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
//// [typeVariableTypeGuards.ts] | ||
// Repro from #14091 | ||
|
||
interface Foo { | ||
foo(): void | ||
} | ||
|
||
class A<P extends Partial<Foo>> { | ||
props: Readonly<P> | ||
doSomething() { | ||
this.props.foo && this.props.foo() | ||
} | ||
} | ||
|
||
// Repro from #14415 | ||
|
||
interface Banana { | ||
color: 'yellow'; | ||
} | ||
|
||
class Monkey<T extends Banana | undefined> { | ||
a: T; | ||
render() { | ||
if (this.a) { | ||
this.a.color; | ||
} | ||
} | ||
} | ||
|
||
interface BigBanana extends Banana { | ||
} | ||
|
||
class BigMonkey extends Monkey<BigBanana> { | ||
render() { | ||
if (this.a) { | ||
this.a.color; | ||
} | ||
} | ||
} | ||
|
||
// Another repro | ||
|
||
type Item = { | ||
(): string; | ||
x: string; | ||
} | ||
|
||
function f1<T extends Item | undefined>(obj: T) { | ||
if (obj) { | ||
obj.x; | ||
obj["x"]; | ||
obj(); | ||
} | ||
} | ||
|
||
function f2<T extends Item | undefined>(obj: T | undefined) { | ||
if (obj) { | ||
obj.x; | ||
obj["x"]; | ||
obj(); | ||
} | ||
} | ||
|
||
function f3<T extends Item | undefined>(obj: T | null) { | ||
if (obj) { | ||
obj.x; | ||
obj["x"]; | ||
obj(); | ||
} | ||
} | ||
|
||
function f4<T extends string[] | undefined>(obj: T | undefined, x: number) { | ||
if (obj) { | ||
obj[x].length; | ||
} | ||
} | ||
|
||
function f5<T, K extends keyof T>(obj: T | undefined, key: K) { | ||
if (obj) { | ||
obj[key]; | ||
} | ||
} | ||
|
||
|
||
//// [typeVariableTypeGuards.js] | ||
"use strict"; | ||
// Repro from #14091 | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; | ||
return function (d, b) { | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
})(); | ||
var A = (function () { | ||
function A() { | ||
} | ||
A.prototype.doSomething = function () { | ||
this.props.foo && this.props.foo(); | ||
}; | ||
return A; | ||
}()); | ||
var Monkey = (function () { | ||
function Monkey() { | ||
} | ||
Monkey.prototype.render = function () { | ||
if (this.a) { | ||
this.a.color; | ||
} | ||
}; | ||
return Monkey; | ||
}()); | ||
var BigMonkey = (function (_super) { | ||
__extends(BigMonkey, _super); | ||
function BigMonkey() { | ||
return _super !== null && _super.apply(this, arguments) || this; | ||
} | ||
BigMonkey.prototype.render = function () { | ||
if (this.a) { | ||
this.a.color; | ||
} | ||
}; | ||
return BigMonkey; | ||
}(Monkey)); | ||
function f1(obj) { | ||
if (obj) { | ||
obj.x; | ||
obj["x"]; | ||
obj(); | ||
} | ||
} | ||
function f2(obj) { | ||
if (obj) { | ||
obj.x; | ||
obj["x"]; | ||
obj(); | ||
} | ||
} | ||
function f3(obj) { | ||
if (obj) { | ||
obj.x; | ||
obj["x"]; | ||
obj(); | ||
} | ||
} | ||
function f4(obj, x) { | ||
if (obj) { | ||
obj[x].length; | ||
} | ||
} | ||
function f5(obj, key) { | ||
if (obj) { | ||
obj[key]; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
&& (<PropertyAccessExpression >parent).expression === node;
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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).