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

Incorporate bang operator hack #22971

Closed
wants to merge 1 commit into from
Closed
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
19 changes: 17 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11205,15 +11205,30 @@ namespace ts {
return type.flags & TypeFlags.Undefined ? type : getUnionType([type, undefinedType]);
}

function isUnconstrainedOrNulllUndefinedConstrainedTypeParameter(type: Type) {
if (!(type.flags & TypeFlags.Instantiable)) return false;
const constraint = getBaseConstraintOfType(type);
if (!constraint) return true;
return maybeTypeOfKind(constraint, TypeFlags.Undefined | TypeFlags.Null);
}

function getGlobalNonNullableTypeInstantiation(type: Type) {
const filtered = getTypeWithFacts(type, TypeFacts.NEUndefinedOrNull); // If type alias unavailable, at least mimic non-higherorder behavior
if (!deferredGlobalNonNullableTypeAlias) {
deferredGlobalNonNullableTypeAlias = getGlobalSymbol("NonNullable" as __String, SymbolFlags.TypeAlias, /*diagnostic*/ undefined) || unknownSymbol;
}
// Use NonNullable global type alias if available to improve quick info/declaration emit
if (deferredGlobalNonNullableTypeAlias !== unknownSymbol) {
return getTypeAliasInstantiation(deferredGlobalNonNullableTypeAlias, [type]);
// The below check prevents us from making a NonNullable<T> where T can't ever be null or undefined
// This feels unnecessesary, as if `T extends Foo`, NonNullable<T> is trivially just `T` (as no
// value of T could ever be nullable); but our conditional type logic is lacking here and can't make
// that leap (it stays generic to allow for, eg T to be instantiated with `never`, which extends `Foo`
// _and_ undefined, thereby choosing a different branch than it would simplify to).
if (forEachType(filtered, isUnconstrainedOrNulllUndefinedConstrainedTypeParameter)) {
return getTypeAliasInstantiation(deferredGlobalNonNullableTypeAlias, [filtered]);
}
}
return getTypeWithFacts(type, TypeFacts.NEUndefinedOrNull); // Type alias unavailable, fall back to non-higherorder behavior
return filtered;
}

function getNonNullableType(type: Type): Type {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//// [bangOperatorRemovesNonNullableWhereSensible.ts]
export class Entry<T extends Table> {
private _table: T | null = null;
createSubsetForDirectory(): void {
const entry = new Entry<T>();
this._table!.fn(entry);
}
}

export abstract class Table {
fn(directoryEntry: Entry<this>): this | null {
return null;
}
}

//// [bangOperatorRemovesNonNullableWhereSensible.js]
"use strict";
exports.__esModule = true;
var Entry = /** @class */ (function () {
function Entry() {
this._table = null;
}
Entry.prototype.createSubsetForDirectory = function () {
var entry = new Entry();
this._table.fn(entry);
};
return Entry;
}());
exports.Entry = Entry;
var Table = /** @class */ (function () {
function Table() {
}
Table.prototype.fn = function (directoryEntry) {
return null;
};
return Table;
}());
exports.Table = Table;
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
=== tests/cases/compiler/bangOperatorRemovesNonNullableWhereSensible.ts ===
export class Entry<T extends Table> {
>Entry : Symbol(Entry, Decl(bangOperatorRemovesNonNullableWhereSensible.ts, 0, 0))
>T : Symbol(T, Decl(bangOperatorRemovesNonNullableWhereSensible.ts, 0, 19))
>Table : Symbol(Table, Decl(bangOperatorRemovesNonNullableWhereSensible.ts, 6, 1))

private _table: T | null = null;
>_table : Symbol(Entry._table, Decl(bangOperatorRemovesNonNullableWhereSensible.ts, 0, 37))
>T : Symbol(T, Decl(bangOperatorRemovesNonNullableWhereSensible.ts, 0, 19))

createSubsetForDirectory(): void {
>createSubsetForDirectory : Symbol(Entry.createSubsetForDirectory, Decl(bangOperatorRemovesNonNullableWhereSensible.ts, 1, 36))

const entry = new Entry<T>();
>entry : Symbol(entry, Decl(bangOperatorRemovesNonNullableWhereSensible.ts, 3, 13))
>Entry : Symbol(Entry, Decl(bangOperatorRemovesNonNullableWhereSensible.ts, 0, 0))
>T : Symbol(T, Decl(bangOperatorRemovesNonNullableWhereSensible.ts, 0, 19))

this._table!.fn(entry);
>this._table!.fn : Symbol(Table.fn, Decl(bangOperatorRemovesNonNullableWhereSensible.ts, 8, 29))
>this._table : Symbol(Entry._table, Decl(bangOperatorRemovesNonNullableWhereSensible.ts, 0, 37))
>this : Symbol(Entry, Decl(bangOperatorRemovesNonNullableWhereSensible.ts, 0, 0))
>_table : Symbol(Entry._table, Decl(bangOperatorRemovesNonNullableWhereSensible.ts, 0, 37))
>fn : Symbol(Table.fn, Decl(bangOperatorRemovesNonNullableWhereSensible.ts, 8, 29))
>entry : Symbol(entry, Decl(bangOperatorRemovesNonNullableWhereSensible.ts, 3, 13))
}
}

export abstract class Table {
>Table : Symbol(Table, Decl(bangOperatorRemovesNonNullableWhereSensible.ts, 6, 1))

fn(directoryEntry: Entry<this>): this | null {
>fn : Symbol(Table.fn, Decl(bangOperatorRemovesNonNullableWhereSensible.ts, 8, 29))
>directoryEntry : Symbol(directoryEntry, Decl(bangOperatorRemovesNonNullableWhereSensible.ts, 9, 7))
>Entry : Symbol(Entry, Decl(bangOperatorRemovesNonNullableWhereSensible.ts, 0, 0))

return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
=== tests/cases/compiler/bangOperatorRemovesNonNullableWhereSensible.ts ===
export class Entry<T extends Table> {
>Entry : Entry<T>
>T : T
>Table : Table

private _table: T | null = null;
>_table : T | null
>T : T
>null : null
>null : null

createSubsetForDirectory(): void {
>createSubsetForDirectory : () => void

const entry = new Entry<T>();
>entry : Entry<T>
>new Entry<T>() : Entry<T>
>Entry : typeof Entry
>T : T

this._table!.fn(entry);
>this._table!.fn(entry) : T | null
>this._table!.fn : (directoryEntry: Entry<T>) => T | null
>this._table! : T
>this._table : T | null
>this : this
>_table : T | null
>fn : (directoryEntry: Entry<T>) => T | null
>entry : Entry<T>
}
}

export abstract class Table {
>Table : Table

fn(directoryEntry: Entry<this>): this | null {
>fn : (directoryEntry: Entry<this>) => this | null
>directoryEntry : Entry<this>
>Entry : Entry<T>
>null : null

return null;
>null : null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function fn<T extends string | undefined, U extends string>(one: T, two: U) {
foo(two!);
>foo(two!) : void
>foo : (p: string) => void
>two! : NonNullable<U>
>two! : U
>two : U

foo(three!); // this line is the important one
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// @strict: true
export class Entry<T extends Table> {
private _table: T | null = null;
createSubsetForDirectory(): void {
const entry = new Entry<T>();
this._table!.fn(entry);
}
}

export abstract class Table {
fn(directoryEntry: Entry<this>): this | null {
return null;
}
}