Skip to content

Commit

Permalink
Explore using a different isArray declaration
Browse files Browse the repository at this point in the history
  • Loading branch information
orta committed Jun 24, 2020
1 parent 785e447 commit 636bc9d
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 9 deletions.
54 changes: 54 additions & 0 deletions src/blank.ts
@@ -0,0 +1,54 @@
declare const MyArray: {
isArray<T>(arg: T | {}): arg is T extends readonly any[] ? readonly any[] : any[];
};

declare const a: readonly string[] | string;
declare const b: string[] | string;
declare const c: unknown;

if (MyArray.isArray(a)) {
a;
}
else {
a;
}
a;

if (MyArray.isArray(b)) {
b;
}
else {
b;
}
b;

if (MyArray.isArray(c)) {
c;
}


function f<T>(x: T) {
const a: readonly T[] | string = null!;
const b: T[] | string = null!;
const c: T = null!;

if (MyArray.isArray(a)) {
a;
}
else {
a;
}
a;

if (MyArray.isArray(b)) {
b;
}
else {
b;
}
b;

if (MyArray.isArray(c)) {
c;
}
}
14 changes: 5 additions & 9 deletions src/compiler/checker.ts
Expand Up @@ -21761,15 +21761,11 @@ namespace ts {
return assignableType;
}
}
// If the candidate type is a subtype of the target type, narrow to the candidate type.
// Otherwise, if the target type is assignable to the candidate type, keep the target type.
// Otherwise, if the candidate type is assignable to the target type, narrow to the candidate
// type. Otherwise, the types are completely unrelated, so narrow to an intersection of the
// two types.
return isTypeSubtypeOf(candidate, type) ? candidate :
isTypeAssignableTo(type, candidate) ? type :
isTypeAssignableTo(candidate, type) ? candidate :
getIntersectionType([type, candidate]);

// If the candidate type is a subtype of the target type, narrow to the candidate type,
// if the target type is a subtype of the candidate type, narrow to the target type,
// otherwise, narrow to an intersection of the two types.
return isTypeSubtypeOf(candidate, type) ? candidate : isTypeSubtypeOf(type, candidate) ? type : getIntersectionType([type, candidate]);
}

function narrowTypeByCallExpression(type: Type, callExpression: CallExpression, assumeTrue: boolean): Type {
Expand Down

0 comments on commit 636bc9d

Please sign in to comment.