-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Workaround for nonnull operator on indexed accesses #19275
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 1 commit
Commits
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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11378,6 +11378,18 @@ namespace ts { | |
} | ||
|
||
function getTypeWithFacts(type: Type, include: TypeFacts) { | ||
if (type.flags & TypeFlags.IndexedAccess) { | ||
// TODO (weswig): This is a substitute for a lazy negated type to remove the types indicated by the TypeFacts from the (potential) union the IndexedAccess refers to | ||
// - instead of defering the resolution of the access, we apply the facts to the base constraint of the access instead; so this works under most circumstances, | ||
// like when the index refers to an optional member and you want to access that member, but unfortunately if that member's type is supposed to vary with the base, | ||
// then the eager evaluation of this removes that relationship | ||
const innerType = getBaseConstraintOfType(type) || emptyObjectType; | ||
|
||
const result = filterType(innerType, t => (getTypeFacts(t) & include) !== 0); | ||
if (result !== innerType) { | ||
return result; | ||
} | ||
return type; | ||
} | ||
return filterType(type, t => (getTypeFacts(t) & include) !== 0); | ||
} | ||
|
||
|
44 changes: 44 additions & 0 deletions
44
tests/baselines/reference/strictNullNotNullIndexTypeShouldWork.js
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
//// [strictNullNotNullIndexTypeShouldWork.ts] | ||
interface A { | ||
params?: { name: string; }; | ||
} | ||
|
||
class Test<T extends A> { | ||
attrs: Readonly<T>; | ||
|
||
m() { | ||
this.attrs.params!.name; | ||
} | ||
} | ||
|
||
interface Foo { | ||
foo?: number; | ||
} | ||
|
||
class FooClass<P extends Foo = Foo> { | ||
properties: Readonly<P>; | ||
|
||
foo(): number { | ||
const { foo = 42 } = this.properties; | ||
return foo; | ||
} | ||
} | ||
|
||
//// [strictNullNotNullIndexTypeShouldWork.js] | ||
var Test = /** @class */ (function () { | ||
function Test() { | ||
} | ||
Test.prototype.m = function () { | ||
this.attrs.params.name; | ||
}; | ||
return Test; | ||
}()); | ||
var FooClass = /** @class */ (function () { | ||
function FooClass() { | ||
} | ||
FooClass.prototype.foo = function () { | ||
var _a = this.properties.foo, foo = _a === void 0 ? 42 : _a; | ||
return foo; | ||
}; | ||
return FooClass; | ||
}()); |
64 changes: 64 additions & 0 deletions
64
tests/baselines/reference/strictNullNotNullIndexTypeShouldWork.symbols
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
=== tests/cases/compiler/strictNullNotNullIndexTypeShouldWork.ts === | ||
interface A { | ||
>A : Symbol(A, Decl(strictNullNotNullIndexTypeShouldWork.ts, 0, 0)) | ||
|
||
params?: { name: string; }; | ||
>params : Symbol(A.params, Decl(strictNullNotNullIndexTypeShouldWork.ts, 0, 13)) | ||
>name : Symbol(name, Decl(strictNullNotNullIndexTypeShouldWork.ts, 1, 14)) | ||
} | ||
|
||
class Test<T extends A> { | ||
>Test : Symbol(Test, Decl(strictNullNotNullIndexTypeShouldWork.ts, 2, 1)) | ||
>T : Symbol(T, Decl(strictNullNotNullIndexTypeShouldWork.ts, 4, 11)) | ||
>A : Symbol(A, Decl(strictNullNotNullIndexTypeShouldWork.ts, 0, 0)) | ||
|
||
attrs: Readonly<T>; | ||
>attrs : Symbol(Test.attrs, Decl(strictNullNotNullIndexTypeShouldWork.ts, 4, 25)) | ||
>Readonly : Symbol(Readonly, Decl(lib.d.ts, --, --)) | ||
>T : Symbol(T, Decl(strictNullNotNullIndexTypeShouldWork.ts, 4, 11)) | ||
|
||
m() { | ||
>m : Symbol(Test.m, Decl(strictNullNotNullIndexTypeShouldWork.ts, 5, 23)) | ||
|
||
this.attrs.params!.name; | ||
>this.attrs.params!.name : Symbol(name, Decl(strictNullNotNullIndexTypeShouldWork.ts, 1, 14)) | ||
>this.attrs.params : Symbol(params, Decl(strictNullNotNullIndexTypeShouldWork.ts, 0, 13)) | ||
>this.attrs : Symbol(Test.attrs, Decl(strictNullNotNullIndexTypeShouldWork.ts, 4, 25)) | ||
>this : Symbol(Test, Decl(strictNullNotNullIndexTypeShouldWork.ts, 2, 1)) | ||
>attrs : Symbol(Test.attrs, Decl(strictNullNotNullIndexTypeShouldWork.ts, 4, 25)) | ||
>params : Symbol(params, Decl(strictNullNotNullIndexTypeShouldWork.ts, 0, 13)) | ||
>name : Symbol(name, Decl(strictNullNotNullIndexTypeShouldWork.ts, 1, 14)) | ||
} | ||
} | ||
|
||
interface Foo { | ||
>Foo : Symbol(Foo, Decl(strictNullNotNullIndexTypeShouldWork.ts, 10, 1)) | ||
|
||
foo?: number; | ||
>foo : Symbol(Foo.foo, Decl(strictNullNotNullIndexTypeShouldWork.ts, 12, 15)) | ||
} | ||
|
||
class FooClass<P extends Foo = Foo> { | ||
>FooClass : Symbol(FooClass, Decl(strictNullNotNullIndexTypeShouldWork.ts, 14, 1)) | ||
>P : Symbol(P, Decl(strictNullNotNullIndexTypeShouldWork.ts, 16, 15)) | ||
>Foo : Symbol(Foo, Decl(strictNullNotNullIndexTypeShouldWork.ts, 10, 1)) | ||
>Foo : Symbol(Foo, Decl(strictNullNotNullIndexTypeShouldWork.ts, 10, 1)) | ||
|
||
properties: Readonly<P>; | ||
>properties : Symbol(FooClass.properties, Decl(strictNullNotNullIndexTypeShouldWork.ts, 16, 37)) | ||
>Readonly : Symbol(Readonly, Decl(lib.d.ts, --, --)) | ||
>P : Symbol(P, Decl(strictNullNotNullIndexTypeShouldWork.ts, 16, 15)) | ||
|
||
foo(): number { | ||
>foo : Symbol(FooClass.foo, Decl(strictNullNotNullIndexTypeShouldWork.ts, 17, 28)) | ||
|
||
const { foo = 42 } = this.properties; | ||
>foo : Symbol(foo, Decl(strictNullNotNullIndexTypeShouldWork.ts, 20, 15)) | ||
>this.properties : Symbol(FooClass.properties, Decl(strictNullNotNullIndexTypeShouldWork.ts, 16, 37)) | ||
>this : Symbol(FooClass, Decl(strictNullNotNullIndexTypeShouldWork.ts, 14, 1)) | ||
>properties : Symbol(FooClass.properties, Decl(strictNullNotNullIndexTypeShouldWork.ts, 16, 37)) | ||
|
||
return foo; | ||
>foo : Symbol(foo, Decl(strictNullNotNullIndexTypeShouldWork.ts, 20, 15)) | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
tests/baselines/reference/strictNullNotNullIndexTypeShouldWork.types
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
=== tests/cases/compiler/strictNullNotNullIndexTypeShouldWork.ts === | ||
interface A { | ||
>A : A | ||
|
||
params?: { name: string; }; | ||
>params : { name: string; } | undefined | ||
>name : string | ||
} | ||
|
||
class Test<T extends A> { | ||
>Test : Test<T> | ||
>T : T | ||
>A : A | ||
|
||
attrs: Readonly<T>; | ||
>attrs : Readonly<T> | ||
>Readonly : Readonly<T> | ||
>T : T | ||
|
||
m() { | ||
>m : () => void | ||
|
||
this.attrs.params!.name; | ||
>this.attrs.params!.name : string | ||
>this.attrs.params! : { name: string; } | ||
>this.attrs.params : T["params"] | ||
>this.attrs : Readonly<T> | ||
>this : this | ||
>attrs : Readonly<T> | ||
>params : T["params"] | ||
>name : string | ||
} | ||
} | ||
|
||
interface Foo { | ||
>Foo : Foo | ||
|
||
foo?: number; | ||
>foo : number | undefined | ||
} | ||
|
||
class FooClass<P extends Foo = Foo> { | ||
>FooClass : FooClass<P> | ||
>P : P | ||
>Foo : Foo | ||
>Foo : Foo | ||
|
||
properties: Readonly<P>; | ||
>properties : Readonly<P> | ||
>Readonly : Readonly<T> | ||
>P : P | ||
|
||
foo(): number { | ||
>foo : () => number | ||
|
||
const { foo = 42 } = this.properties; | ||
>foo : number | ||
>42 : 42 | ||
>this.properties : Readonly<P> | ||
>this : this | ||
>properties : Readonly<P> | ||
|
||
return foo; | ||
>foo : number | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
tests/cases/compiler/strictNullNotNullIndexTypeShouldWork.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// @strictNullChecks: true | ||
interface A { | ||
params?: { name: string; }; | ||
} | ||
|
||
class Test<T extends A> { | ||
attrs: Readonly<T>; | ||
|
||
m() { | ||
this.attrs.params!.name; | ||
} | ||
} | ||
|
||
interface Foo { | ||
foo?: number; | ||
} | ||
|
||
class FooClass<P extends Foo = Foo> { | ||
properties: Readonly<P>; | ||
|
||
foo(): number { | ||
const { foo = 42 } = this.properties; | ||
return foo; | ||
} | ||
} |
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.