-
Notifications
You must be signed in to change notification settings - Fork 12.8k
check return type of this type predicates #57341
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
62072f4
check return type of this type predicates
Zzzen 89a7208
format code
Zzzen b811562
stricter check
Zzzen 4c9edfe
Merge remote-tracking branch 'origin/main' into check-this-predicate
Zzzen c068296
update baseline
Zzzen f889bfc
update baseline
Zzzen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
tests/baselines/reference/implementArrayInterface.errors.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
implementArrayInterface.ts(19,5): error TS2416: Property 'every' in type 'MyArray<T>' is not assignable to the same property in base type 'T[]'. | ||
Type '(callbackfn: (value: T, index: number, array: T[]) => boolean, thisArg?: any) => boolean' is not assignable to type '{ <S extends T>(predicate: (value: T, index: number, array: T[]) => value is S, thisArg?: any): this is S[]; (predicate: (value: T, index: number, array: T[]) => unknown, thisArg?: any): boolean; }'. | ||
Signature '(callbackfn: (value: T, index: number, array: T[]) => boolean, thisArg?: any): boolean' must be a type predicate. | ||
|
||
|
||
==== implementArrayInterface.ts (1 errors) ==== | ||
declare class MyArray<T> implements Array<T> { | ||
toString(): string; | ||
toLocaleString(): string; | ||
concat<U extends T[]>(...items: U[]): T[]; | ||
concat(...items: T[]): T[]; | ||
join(separator?: string): string; | ||
pop(): T; | ||
push(...items: T[]): number; | ||
reverse(): T[]; | ||
shift(): T; | ||
slice(start?: number, end?: number): T[]; | ||
sort(compareFn?: (a: T, b: T) => number): this; | ||
splice(start: number): T[]; | ||
splice(start: number, deleteCount: number, ...items: T[]): T[]; | ||
unshift(...items: T[]): number; | ||
|
||
indexOf(searchElement: T, fromIndex?: number): number; | ||
lastIndexOf(searchElement: T, fromIndex?: number): number; | ||
every(callbackfn: (value: T, index: number, array: T[]) => boolean, thisArg?: any): boolean; | ||
~~~~~ | ||
!!! error TS2416: Property 'every' in type 'MyArray<T>' is not assignable to the same property in base type 'T[]'. | ||
!!! error TS2416: Type '(callbackfn: (value: T, index: number, array: T[]) => boolean, thisArg?: any) => boolean' is not assignable to type '{ <S extends T>(predicate: (value: T, index: number, array: T[]) => value is S, thisArg?: any): this is S[]; (predicate: (value: T, index: number, array: T[]) => unknown, thisArg?: any): boolean; }'. | ||
!!! error TS2416: Signature '(callbackfn: (value: T, index: number, array: T[]) => boolean, thisArg?: any): boolean' must be a type predicate. | ||
some(callbackfn: (value: T, index: number, array: T[]) => boolean, thisArg?: any): boolean; | ||
forEach(callbackfn: (value: T, index: number, array: T[]) => void, thisArg?: any): void; | ||
map<U>(callbackfn: (value: T, index: number, array: T[]) => U, thisArg?: any): U[]; | ||
filter(callbackfn: (value: T, index: number, array: T[]) => boolean, thisArg?: any): T[]; | ||
reduce(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue?: T): T; | ||
reduce<U>(callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U, initialValue: U): U; | ||
reduceRight(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue?: T): T; | ||
reduceRight<U>(callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U, initialValue: U): U; | ||
|
||
length: number; | ||
|
||
[n: number]: T; | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
typePredicateInherit.ts(11,3): error TS2416: Property 'method1' in type 'B' is not assignable to the same property in base type 'A'. | ||
Type '() => void' is not assignable to type '() => this is { a: 1; }'. | ||
Signature '(): void' must be a type predicate. | ||
typePredicateInherit.ts(13,3): error TS2416: Property 'method2' in type 'B' is not assignable to the same property in base type 'A'. | ||
Type '() => void' is not assignable to type '() => boolean'. | ||
Type 'void' is not assignable to type 'boolean'. | ||
typePredicateInherit.ts(15,3): error TS2416: Property 'method3' in type 'B' is not assignable to the same property in base type 'A'. | ||
Type '() => boolean' is not assignable to type '() => this is { a: 1; }'. | ||
Signature '(): boolean' must be a type predicate. | ||
typePredicateInherit.ts(41,3): error TS2416: Property 'method1' in type 'D' is not assignable to the same property in base type 'C'. | ||
Type '() => void' is not assignable to type '() => this is { a: 1; }'. | ||
Signature '(): void' must be a type predicate. | ||
typePredicateInherit.ts(50,3): error TS2416: Property 'method3' in type 'D' is not assignable to the same property in base type 'C'. | ||
Type '() => boolean' is not assignable to type '() => this is { a: 1; }'. | ||
Signature '(): boolean' must be a type predicate. | ||
|
||
|
||
==== typePredicateInherit.ts (5 errors) ==== | ||
interface A { | ||
method1(): this is { | ||
a: 1 | ||
} | ||
method2(): boolean; | ||
method3(): this is { | ||
a: 1 | ||
}; | ||
} | ||
class B implements A { | ||
method1() { } // should error | ||
~~~~~~~ | ||
!!! error TS2416: Property 'method1' in type 'B' is not assignable to the same property in base type 'A'. | ||
!!! error TS2416: Type '() => void' is not assignable to type '() => this is { a: 1; }'. | ||
!!! error TS2416: Signature '(): void' must be a type predicate. | ||
|
||
method2() { } // should error | ||
~~~~~~~ | ||
!!! error TS2416: Property 'method2' in type 'B' is not assignable to the same property in base type 'A'. | ||
!!! error TS2416: Type '() => void' is not assignable to type '() => boolean'. | ||
!!! error TS2416: Type 'void' is not assignable to type 'boolean'. | ||
|
||
method3() { // should error | ||
~~~~~~~ | ||
!!! error TS2416: Property 'method3' in type 'B' is not assignable to the same property in base type 'A'. | ||
!!! error TS2416: Type '() => boolean' is not assignable to type '() => this is { a: 1; }'. | ||
!!! error TS2416: Signature '(): boolean' must be a type predicate. | ||
return true | ||
} | ||
} | ||
|
||
class C { | ||
method1(): this is { | ||
a: 1 | ||
} { | ||
return true; | ||
} | ||
|
||
method2(): this is { | ||
a: 1 | ||
} { | ||
return true; | ||
} | ||
|
||
method3(): this is { | ||
a: 1 | ||
} { | ||
return true; | ||
} | ||
} | ||
|
||
class D extends C { | ||
method1(): void { // should error | ||
~~~~~~~ | ||
!!! error TS2416: Property 'method1' in type 'D' is not assignable to the same property in base type 'C'. | ||
!!! error TS2416: Type '() => void' is not assignable to type '() => this is { a: 1; }'. | ||
!!! error TS2416: Signature '(): void' must be a type predicate. | ||
} | ||
|
||
method2(): this is { // should ok | ||
a: 1 | ||
} { | ||
return true; | ||
} | ||
|
||
method3(): boolean { // should error | ||
~~~~~~~ | ||
!!! error TS2416: Property 'method3' in type 'D' is not assignable to the same property in base type 'C'. | ||
!!! error TS2416: Type '() => boolean' is not assignable to type '() => this is { a: 1; }'. | ||
!!! error TS2416: Signature '(): boolean' must be a type predicate. | ||
return true; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
//// [tests/cases/compiler/typePredicateInherit.ts] //// | ||
|
||
//// [typePredicateInherit.ts] | ||
interface A { | ||
method1(): this is { | ||
a: 1 | ||
} | ||
method2(): boolean; | ||
method3(): this is { | ||
a: 1 | ||
}; | ||
} | ||
class B implements A { | ||
method1() { } // should error | ||
|
||
method2() { } // should error | ||
|
||
method3() { // should error | ||
return true | ||
} | ||
} | ||
|
||
class C { | ||
method1(): this is { | ||
a: 1 | ||
} { | ||
return true; | ||
} | ||
|
||
method2(): this is { | ||
a: 1 | ||
} { | ||
return true; | ||
} | ||
|
||
method3(): this is { | ||
a: 1 | ||
} { | ||
return true; | ||
} | ||
} | ||
|
||
class D extends C { | ||
method1(): void { // should error | ||
} | ||
|
||
method2(): this is { // should ok | ||
a: 1 | ||
} { | ||
return true; | ||
} | ||
|
||
method3(): boolean { // should error | ||
return true; | ||
} | ||
} | ||
|
||
//// [typePredicateInherit.js] | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = function (d, b) { | ||
extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; | ||
return extendStatics(d, b); | ||
}; | ||
return function (d, b) { | ||
if (typeof b !== "function" && b !== null) | ||
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
})(); | ||
var B = /** @class */ (function () { | ||
function B() { | ||
} | ||
B.prototype.method1 = function () { }; // should error | ||
B.prototype.method2 = function () { }; // should error | ||
B.prototype.method3 = function () { | ||
return true; | ||
}; | ||
return B; | ||
}()); | ||
var C = /** @class */ (function () { | ||
function C() { | ||
} | ||
C.prototype.method1 = function () { | ||
return true; | ||
}; | ||
C.prototype.method2 = function () { | ||
return true; | ||
}; | ||
C.prototype.method3 = function () { | ||
return true; | ||
}; | ||
return C; | ||
}()); | ||
var D = /** @class */ (function (_super) { | ||
__extends(D, _super); | ||
function D() { | ||
return _super !== null && _super.apply(this, arguments) || this; | ||
} | ||
D.prototype.method1 = function () { | ||
}; | ||
D.prototype.method2 = function () { | ||
return true; | ||
}; | ||
D.prototype.method3 = function () { | ||
return true; | ||
}; | ||
return D; | ||
}(C)); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😦