Skip to content

Allow filterType to consider union constraints of non-union types when determining never-ness #43763

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 4 commits into from
May 5, 2021
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
11 changes: 10 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23766,7 +23766,16 @@ namespace ts {

function getNarrowedType(type: Type, candidate: Type, assumeTrue: boolean, isRelated: (source: Type, target: Type) => boolean) {
if (!assumeTrue) {
return filterType(type, t => !isRelated(t, candidate));
return filterType(type, t => {
if (!isRelated(t, candidate)) {
return true;
}
const constraint = getBaseConstraintOfType(t);
if (constraint && constraint !== t) {
return !isRelated(constraint, candidate);
}
return false;
});
}
// If the current type is a union type, remove all constituents that couldn't be instances of
// the candidate type. If one or more constituents remain, return a union of those.
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ namespace ts {
return `${newLine}${flattenDiagnosticMessageText(d.messageText, newLine)}${newLine}${newLine}`;
}

export function isBuilderProgram<T extends BuilderProgram>(program: Program | T): program is T {
return !!(program as T).getState;
export function isBuilderProgram(program: Program | BuilderProgram): program is BuilderProgram {
return !!(program as BuilderProgram).getState;
}

export function listFiles<T extends BuilderProgram>(program: Program | T, write: (s: string) => void) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
tests/cases/compiler/quickinfoTypeAtReturnPositionsInaccurate.ts(33,15): error TS2339: Property 'numExclusive' does not exist on type 'NumClass<number> | StrClass<string>'.
Property 'numExclusive' does not exist on type 'StrClass<string>'.
tests/cases/compiler/quickinfoTypeAtReturnPositionsInaccurate.ts(101,11): error TS2322: Type 'Program | T' is not assignable to type 'Program'.
Type 'T' is not assignable to type 'Program'.
Property 'state' is missing in type 'BuilderProgram' but required in type 'Program'.


==== tests/cases/compiler/quickinfoTypeAtReturnPositionsInaccurate.ts (2 errors) ====
class NumClass<T extends number> {
private value!: T;
public get(): T {
return this.value;
}
public numExclusive() { }
}

class StrClass<T extends string> {
private value!: T;
public get(): T {
return this.value;
}
public strExclusive() { }
}

const isNumClass = <Item extends NumClass<number> | StrClass<string>> (
item: Item
): item is Extract<Item, NumClass<any>> => {
return (item instanceof NumClass);
}

/**
* An example with one dimensional dictionary. Everything worked ok here, even in prior
* versions.
*/
class SimpleStore<Entries extends { [index: string]: NumClass<number> | StrClass<string> }> {
private entries = { } as Entries;

public get<EntryId extends keyof Entries>(entryId: EntryId): Entries[EntryId] {
let entry = this.entries[entryId];

entry.numExclusive(); // error - expected.
~~~~~~~~~~~~
!!! error TS2339: Property 'numExclusive' does not exist on type 'NumClass<number> | StrClass<string>'.
!!! error TS2339: Property 'numExclusive' does not exist on type 'StrClass<string>'.

if (isNumClass(entry)) {
entry.numExclusive(); // works
return entry;
}

return entry; // type is Entries[EntryId] - all fine
}
}

type Slice = {
[index: string]: NumClass<number> | StrClass<string>
}

/**
* A an example with 2-dimensional dictionary.
*
* In v4.1 the `isNumClass` type guard doesn't work at all.
* In v4.2 or later, `isNumClass` type guard leaks outside its
* scope.
*/
class ComplexStore<Slices extends { [index: string]: Slice }> {
private slices = { } as Slices;

public get<SliceId extends keyof Slices, SliceKey extends keyof Slices[SliceId]>(
sliceId: SliceId, sliceKey: SliceKey
): Slices[SliceId][SliceKey] {
let item = this.slices[sliceId][sliceKey];

if (isNumClass(item)) {
item.numExclusive(); // works only since version 4.2
}

item.get();

// unfortunately, doesn't work completely.
// it seems like item's predicated type leaks outside the bracket...

return item; // type is Extract ...
}

public get2<SliceId extends keyof Slices, SliceKey extends keyof Slices[SliceId]>(
sliceId: SliceId, sliceKey: SliceKey
): Slices[SliceId][SliceKey] {
let item = this.slices[sliceId][sliceKey];

if (isNumClass(item)) {
return item;
}
// it seems like the compiler asumes the above condition is always
// truthy

item.get();

return item; // type is never
}
}

// from the compiler itself
interface BuilderProgram {
getProgram(): Program;
}
interface Program {
state: any;
}
declare function isBuilderProgram<T extends BuilderProgram>(program: Program | T): program is T;
export function listFiles<T extends BuilderProgram>(program: Program | T) {
const x: Program = isBuilderProgram(program) ? program.getProgram() : program;
~
!!! error TS2322: Type 'Program | T' is not assignable to type 'Program'.
!!! error TS2322: Type 'T' is not assignable to type 'Program'.
!!! error TS2322: Property 'state' is missing in type 'BuilderProgram' but required in type 'Program'.
!!! related TS2728 tests/cases/compiler/quickinfoTypeAtReturnPositionsInaccurate.ts:97:5: 'state' is declared here.
}
185 changes: 185 additions & 0 deletions tests/baselines/reference/quickinfoTypeAtReturnPositionsInaccurate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
//// [quickinfoTypeAtReturnPositionsInaccurate.ts]
class NumClass<T extends number> {
private value!: T;
public get(): T {
return this.value;
}
public numExclusive() { }
}

class StrClass<T extends string> {
private value!: T;
public get(): T {
return this.value;
}
public strExclusive() { }
}

const isNumClass = <Item extends NumClass<number> | StrClass<string>> (
item: Item
): item is Extract<Item, NumClass<any>> => {
return (item instanceof NumClass);
}

/**
* An example with one dimensional dictionary. Everything worked ok here, even in prior
* versions.
*/
class SimpleStore<Entries extends { [index: string]: NumClass<number> | StrClass<string> }> {
private entries = { } as Entries;

public get<EntryId extends keyof Entries>(entryId: EntryId): Entries[EntryId] {
let entry = this.entries[entryId];

entry.numExclusive(); // error - expected.

if (isNumClass(entry)) {
entry.numExclusive(); // works
return entry;
}

return entry; // type is Entries[EntryId] - all fine
}
}

type Slice = {
[index: string]: NumClass<number> | StrClass<string>
}

/**
* A an example with 2-dimensional dictionary.
*
* In v4.1 the `isNumClass` type guard doesn't work at all.
* In v4.2 or later, `isNumClass` type guard leaks outside its
* scope.
*/
class ComplexStore<Slices extends { [index: string]: Slice }> {
private slices = { } as Slices;

public get<SliceId extends keyof Slices, SliceKey extends keyof Slices[SliceId]>(
sliceId: SliceId, sliceKey: SliceKey
): Slices[SliceId][SliceKey] {
let item = this.slices[sliceId][sliceKey];

if (isNumClass(item)) {
item.numExclusive(); // works only since version 4.2
}

item.get();

// unfortunately, doesn't work completely.
// it seems like item's predicated type leaks outside the bracket...

return item; // type is Extract ...
}

public get2<SliceId extends keyof Slices, SliceKey extends keyof Slices[SliceId]>(
sliceId: SliceId, sliceKey: SliceKey
): Slices[SliceId][SliceKey] {
let item = this.slices[sliceId][sliceKey];

if (isNumClass(item)) {
return item;
}
// it seems like the compiler asumes the above condition is always
// truthy

item.get();

return item; // type is never
}
}

// from the compiler itself
interface BuilderProgram {
getProgram(): Program;
}
interface Program {
state: any;
}
declare function isBuilderProgram<T extends BuilderProgram>(program: Program | T): program is T;
export function listFiles<T extends BuilderProgram>(program: Program | T) {
const x: Program = isBuilderProgram(program) ? program.getProgram() : program;
}

//// [quickinfoTypeAtReturnPositionsInaccurate.js]
"use strict";
exports.__esModule = true;
exports.listFiles = void 0;
var NumClass = /** @class */ (function () {
function NumClass() {
}
NumClass.prototype.get = function () {
return this.value;
};
NumClass.prototype.numExclusive = function () { };
return NumClass;
}());
var StrClass = /** @class */ (function () {
function StrClass() {
}
StrClass.prototype.get = function () {
return this.value;
};
StrClass.prototype.strExclusive = function () { };
return StrClass;
}());
var isNumClass = function (item) {
return (item instanceof NumClass);
};
/**
* An example with one dimensional dictionary. Everything worked ok here, even in prior
* versions.
*/
var SimpleStore = /** @class */ (function () {
function SimpleStore() {
this.entries = {};
}
SimpleStore.prototype.get = function (entryId) {
var entry = this.entries[entryId];
entry.numExclusive(); // error - expected.
if (isNumClass(entry)) {
entry.numExclusive(); // works
return entry;
}
return entry; // type is Entries[EntryId] - all fine
};
return SimpleStore;
}());
/**
* A an example with 2-dimensional dictionary.
*
* In v4.1 the `isNumClass` type guard doesn't work at all.
* In v4.2 or later, `isNumClass` type guard leaks outside its
* scope.
*/
var ComplexStore = /** @class */ (function () {
function ComplexStore() {
this.slices = {};
}
ComplexStore.prototype.get = function (sliceId, sliceKey) {
var item = this.slices[sliceId][sliceKey];
if (isNumClass(item)) {
item.numExclusive(); // works only since version 4.2
}
item.get();
// unfortunately, doesn't work completely.
// it seems like item's predicated type leaks outside the bracket...
return item; // type is Extract ...
};
ComplexStore.prototype.get2 = function (sliceId, sliceKey) {
var item = this.slices[sliceId][sliceKey];
if (isNumClass(item)) {
return item;
}
// it seems like the compiler asumes the above condition is always
// truthy
item.get();
return item; // type is never
};
return ComplexStore;
}());
function listFiles(program) {
var x = isBuilderProgram(program) ? program.getProgram() : program;
}
exports.listFiles = listFiles;
Loading