Skip to content
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

Workaround for nonnull operator on indexed accesses #19275

Merged
merged 3 commits into from
Oct 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 10 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11378,6 +11378,16 @@ 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
// - See discussion in https://github.com/Microsoft/TypeScript/pull/19275 for details, and test `strictNullNotNullIndexTypeShouldWork` for current behavior
const baseConstraint = getBaseConstraintOfType(type) || emptyObjectType;
const result = filterType(baseConstraint, t => (getTypeFacts(t) & include) !== 0);
if (result !== baseConstraint) {
return result;
}
return type;
}
return filterType(type, t => (getTypeFacts(t) & include) !== 0);
}

Expand Down
60 changes: 60 additions & 0 deletions tests/baselines/reference/strictNullNotNullIndexTypeShouldWork.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//// [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;
}
}

class Test2<T extends A> {
attrs: Readonly<T>;

m() {
return this.attrs.params!; // Return type should maintain relationship with `T` after being not-null-asserted, ideally
}
}

//// [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;
}());
var Test2 = /** @class */ (function () {
function Test2() {
}
Test2.prototype.m = function () {
return this.attrs.params; // Return type should maintain relationship with `T` after being not-null-asserted, ideally
};
return Test2;
}());
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
=== 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))
}
}

class Test2<T extends A> {
>Test2 : Symbol(Test2, Decl(strictNullNotNullIndexTypeShouldWork.ts, 23, 1))
>T : Symbol(T, Decl(strictNullNotNullIndexTypeShouldWork.ts, 25, 12))
>A : Symbol(A, Decl(strictNullNotNullIndexTypeShouldWork.ts, 0, 0))

attrs: Readonly<T>;
>attrs : Symbol(Test2.attrs, Decl(strictNullNotNullIndexTypeShouldWork.ts, 25, 26))
>Readonly : Symbol(Readonly, Decl(lib.d.ts, --, --))
>T : Symbol(T, Decl(strictNullNotNullIndexTypeShouldWork.ts, 25, 12))

m() {
>m : Symbol(Test2.m, Decl(strictNullNotNullIndexTypeShouldWork.ts, 26, 23))

return this.attrs.params!; // Return type should maintain relationship with `T` after being not-null-asserted, ideally
>this.attrs.params : Symbol(params, Decl(strictNullNotNullIndexTypeShouldWork.ts, 0, 13))
>this.attrs : Symbol(Test2.attrs, Decl(strictNullNotNullIndexTypeShouldWork.ts, 25, 26))
>this : Symbol(Test2, Decl(strictNullNotNullIndexTypeShouldWork.ts, 23, 1))
>attrs : Symbol(Test2.attrs, Decl(strictNullNotNullIndexTypeShouldWork.ts, 25, 26))
>params : Symbol(params, Decl(strictNullNotNullIndexTypeShouldWork.ts, 0, 13))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
=== 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
}
}

class Test2<T extends A> {
>Test2 : Test2<T>
>T : T
>A : A

attrs: Readonly<T>;
>attrs : Readonly<T>
>Readonly : Readonly<T>
>T : T

m() {
>m : () => { name: string; }

return this.attrs.params!; // Return type should maintain relationship with `T` after being not-null-asserted, ideally
>this.attrs.params! : { name: string; }
>this.attrs.params : T["params"]
>this.attrs : Readonly<T>
>this : this
>attrs : Readonly<T>
>params : T["params"]
}
}
33 changes: 33 additions & 0 deletions tests/cases/compiler/strictNullNotNullIndexTypeShouldWork.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// @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;
}
}

class Test2<T extends A> {
attrs: Readonly<T>;

m() {
return this.attrs.params!; // Return type should maintain relationship with `T` after being not-null-asserted, ideally
}
}