Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

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

  1. Can you add a short comment to the test that links the test case to this limitation?
  2. Can you put in an example here?
  3. Can you use fewer words if you have an example?

const innerType = getBaseConstraintOfType(type) || emptyObjectType;
Copy link
Member

Choose a reason for hiding this comment

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

Name: constraint or baseConstraint?

const result = filterType(innerType, t => (getTypeFacts(t) & include) !== 0);
if (result !== innerType) {
return result;
}
return type;
}
return filterType(type, t => (getTypeFacts(t) & include) !== 0);
}

Expand Down
44 changes: 44 additions & 0 deletions tests/baselines/reference/strictNullNotNullIndexTypeShouldWork.js
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;
}());
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))
}
}
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 tests/cases/compiler/strictNullNotNullIndexTypeShouldWork.ts
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;
}
}