From 62072f4991471288182df4ac9377128726e3a178 Mon Sep 17 00:00:00 2001 From: Zzzen Date: Thu, 8 Feb 2024 23:17:12 +0800 Subject: [PATCH 1/5] check return type of this type predicates --- src/compiler/checker.ts | 3 + .../reference/typePredicateInherit.errors.txt | 77 ++++++++++++ .../reference/typePredicateInherit.js | 112 ++++++++++++++++++ .../reference/typePredicateInherit.symbols | 98 +++++++++++++++ .../reference/typePredicateInherit.types | 101 ++++++++++++++++ tests/cases/compiler/typePredicateInherit.ts | 53 +++++++++ 6 files changed, 444 insertions(+) create mode 100644 tests/baselines/reference/typePredicateInherit.errors.txt create mode 100644 tests/baselines/reference/typePredicateInherit.js create mode 100644 tests/baselines/reference/typePredicateInherit.symbols create mode 100644 tests/baselines/reference/typePredicateInherit.types create mode 100644 tests/cases/compiler/typePredicateInherit.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 91a5c09454b09..a78a538b38049 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -729,6 +729,7 @@ import { isThisInTypeQuery, isThisProperty, isThisTypeParameter, + isThisTypePredicate, isTransientSymbol, isTupleTypeNode, isTypeAlias, @@ -20887,6 +20888,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { errorReporter!(Diagnostics.Signature_0_must_be_a_type_predicate, signatureToString(source)); } return Ternary.False; + } else if (isThisTypePredicate(targetTypePredicate)) { + result &= compareTypes(sourceReturnType, targetReturnType, reportErrors); } } else { diff --git a/tests/baselines/reference/typePredicateInherit.errors.txt b/tests/baselines/reference/typePredicateInherit.errors.txt new file mode 100644 index 0000000000000..cd5f1e6db173d --- /dev/null +++ b/tests/baselines/reference/typePredicateInherit.errors.txt @@ -0,0 +1,77 @@ +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; }'. + Type 'void' is not assignable to type 'boolean'. +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(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; }'. + Type 'void' is not assignable to type 'boolean'. + + +==== typePredicateInherit.ts (3 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: Type 'void' is not assignable to type 'boolean'. + + 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 ok + 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: Type 'void' is not assignable to type 'boolean'. + } + + method2(): this is { // should ok + a: 1 + } { + return true; + } + + method3(): boolean { // should ok + return true; + } + } \ No newline at end of file diff --git a/tests/baselines/reference/typePredicateInherit.js b/tests/baselines/reference/typePredicateInherit.js new file mode 100644 index 0000000000000..e19fa14be8915 --- /dev/null +++ b/tests/baselines/reference/typePredicateInherit.js @@ -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 ok + 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 ok + 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)); diff --git a/tests/baselines/reference/typePredicateInherit.symbols b/tests/baselines/reference/typePredicateInherit.symbols new file mode 100644 index 0000000000000..1777be5038254 --- /dev/null +++ b/tests/baselines/reference/typePredicateInherit.symbols @@ -0,0 +1,98 @@ +//// [tests/cases/compiler/typePredicateInherit.ts] //// + +=== typePredicateInherit.ts === +interface A { +>A : Symbol(A, Decl(typePredicateInherit.ts, 0, 0)) + + method1(): this is { +>method1 : Symbol(A.method1, Decl(typePredicateInherit.ts, 0, 13)) + + a: 1 +>a : Symbol(a, Decl(typePredicateInherit.ts, 1, 22)) + } + method2(): boolean; +>method2 : Symbol(A.method2, Decl(typePredicateInherit.ts, 3, 3)) + + method3(): this is { +>method3 : Symbol(A.method3, Decl(typePredicateInherit.ts, 4, 21)) + + a: 1 +>a : Symbol(a, Decl(typePredicateInherit.ts, 5, 22)) + + }; +} +class B implements A { +>B : Symbol(B, Decl(typePredicateInherit.ts, 8, 1)) +>A : Symbol(A, Decl(typePredicateInherit.ts, 0, 0)) + + method1() { } // should error +>method1 : Symbol(B.method1, Decl(typePredicateInherit.ts, 9, 22)) + + method2() { } // should error +>method2 : Symbol(B.method2, Decl(typePredicateInherit.ts, 10, 15)) + + method3() { // should ok +>method3 : Symbol(B.method3, Decl(typePredicateInherit.ts, 12, 15)) + + return true + } +} + +class C { +>C : Symbol(C, Decl(typePredicateInherit.ts, 17, 1)) + + method1(): this is { +>method1 : Symbol(C.method1, Decl(typePredicateInherit.ts, 19, 9)) + + a: 1 +>a : Symbol(a, Decl(typePredicateInherit.ts, 20, 22)) + + } { + return true; + } + + method2(): this is { +>method2 : Symbol(C.method2, Decl(typePredicateInherit.ts, 24, 3)) + + a: 1 +>a : Symbol(a, Decl(typePredicateInherit.ts, 26, 22)) + + } { + return true; + } + + method3(): this is { +>method3 : Symbol(C.method3, Decl(typePredicateInherit.ts, 30, 3)) + + a: 1 +>a : Symbol(a, Decl(typePredicateInherit.ts, 32, 22)) + + } { + return true; + } +} + +class D extends C { +>D : Symbol(D, Decl(typePredicateInherit.ts, 37, 1)) +>C : Symbol(C, Decl(typePredicateInherit.ts, 17, 1)) + + method1(): void { // should error +>method1 : Symbol(D.method1, Decl(typePredicateInherit.ts, 39, 19)) + } + + method2(): this is { // should ok +>method2 : Symbol(D.method2, Decl(typePredicateInherit.ts, 41, 3)) + + a: 1 +>a : Symbol(a, Decl(typePredicateInherit.ts, 43, 22)) + + } { + return true; + } + + method3(): boolean { // should ok +>method3 : Symbol(D.method3, Decl(typePredicateInherit.ts, 47, 3)) + + return true; + } +} diff --git a/tests/baselines/reference/typePredicateInherit.types b/tests/baselines/reference/typePredicateInherit.types new file mode 100644 index 0000000000000..6149a44008824 --- /dev/null +++ b/tests/baselines/reference/typePredicateInherit.types @@ -0,0 +1,101 @@ +//// [tests/cases/compiler/typePredicateInherit.ts] //// + +=== typePredicateInherit.ts === +interface A { + method1(): this is { +>method1 : () => this is { a: 1; } + + a: 1 +>a : 1 + } + method2(): boolean; +>method2 : () => boolean + + method3(): this is { +>method3 : () => this is { a: 1; } + + a: 1 +>a : 1 + + }; +} +class B implements A { +>B : B + + method1() { } // should error +>method1 : () => void + + method2() { } // should error +>method2 : () => void + + method3() { // should ok +>method3 : () => boolean + + return true +>true : true + } +} + +class C { +>C : C + + method1(): this is { +>method1 : () => this is { a: 1; } + + a: 1 +>a : 1 + + } { + return true; +>true : true + } + + method2(): this is { +>method2 : () => this is { a: 1; } + + a: 1 +>a : 1 + + } { + return true; +>true : true + } + + method3(): this is { +>method3 : () => this is { a: 1; } + + a: 1 +>a : 1 + + } { + return true; +>true : true + } +} + +class D extends C { +>D : D +>C : C + + method1(): void { // should error +>method1 : () => void + } + + method2(): this is { // should ok +>method2 : () => this is { a: 1; } + + a: 1 +>a : 1 + + } { + return true; +>true : true + } + + method3(): boolean { // should ok +>method3 : () => boolean + + return true; +>true : true + } +} diff --git a/tests/cases/compiler/typePredicateInherit.ts b/tests/cases/compiler/typePredicateInherit.ts new file mode 100644 index 0000000000000..213e58b48af1b --- /dev/null +++ b/tests/cases/compiler/typePredicateInherit.ts @@ -0,0 +1,53 @@ +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 ok + 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 ok + return true; + } +} \ No newline at end of file From 89a7208819a46178f958a96138a5d3b90e514277 Mon Sep 17 00:00:00 2001 From: Zzzen Date: Thu, 8 Feb 2024 23:20:07 +0800 Subject: [PATCH 2/5] format code --- src/compiler/checker.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index a78a538b38049..c2713fb946551 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -20888,7 +20888,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { errorReporter!(Diagnostics.Signature_0_must_be_a_type_predicate, signatureToString(source)); } return Ternary.False; - } else if (isThisTypePredicate(targetTypePredicate)) { + } + else if (isThisTypePredicate(targetTypePredicate)) { result &= compareTypes(sourceReturnType, targetReturnType, reportErrors); } } From b811562ef26d08bd0395531261ab8e94591d8ebf Mon Sep 17 00:00:00 2001 From: Zzzen Date: Sat, 6 Apr 2024 16:35:25 +0800 Subject: [PATCH 3/5] stricter check --- src/compiler/checker.ts | 5 +-- src/server/scriptVersionCache.ts | 4 +- .../implementArrayInterface.errors.txt | 43 +++++++++++++++++++ .../reference/typePredicateInherit.errors.txt | 28 +++++++++--- .../reference/typePredicateInherit.js | 4 +- .../reference/typePredicateInherit.symbols | 4 +- .../reference/typePredicateInherit.types | 4 +- tests/cases/compiler/typePredicateInherit.ts | 4 +- 8 files changed, 75 insertions(+), 21 deletions(-) create mode 100644 tests/baselines/reference/implementArrayInterface.errors.txt diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index c2713fb946551..0b09f56a4d2ee 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -20883,15 +20883,12 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { if (sourceTypePredicate) { result &= compareTypePredicateRelatedTo(sourceTypePredicate, targetTypePredicate, reportErrors, errorReporter, compareTypes); } - else if (isIdentifierTypePredicate(targetTypePredicate)) { + else if (isIdentifierTypePredicate(targetTypePredicate) || isThisTypePredicate(targetTypePredicate)) { if (reportErrors) { errorReporter!(Diagnostics.Signature_0_must_be_a_type_predicate, signatureToString(source)); } return Ternary.False; } - else if (isThisTypePredicate(targetTypePredicate)) { - result &= compareTypes(sourceReturnType, targetReturnType, reportErrors); - } } else { // When relating callback signatures, we still need to relate return types bi-variantly as otherwise diff --git a/src/server/scriptVersionCache.ts b/src/server/scriptVersionCache.ts index 28449179243cf..81d6d15e36ecf 100644 --- a/src/server/scriptVersionCache.ts +++ b/src/server/scriptVersionCache.ts @@ -593,7 +593,7 @@ export class LineNode implements LineCollection { if (children.length) this.updateCounts(); } - isLeaf() { + isLeaf(): this is LineLeaf { return false; } @@ -841,7 +841,7 @@ export class LineLeaf implements LineCollection { constructor(public text: string) { } - isLeaf() { + isLeaf(): this is LineLeaf { return true; } diff --git a/tests/baselines/reference/implementArrayInterface.errors.txt b/tests/baselines/reference/implementArrayInterface.errors.txt new file mode 100644 index 0000000000000..4d6b297e6093a --- /dev/null +++ b/tests/baselines/reference/implementArrayInterface.errors.txt @@ -0,0 +1,43 @@ +implementArrayInterface.ts(19,5): error TS2416: Property 'every' in type 'MyArray' 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 '{ (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 implements Array { + toString(): string; + toLocaleString(): string; + concat(...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' 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 '{ (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(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(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(callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U, initialValue: U): U; + + length: number; + + [n: number]: T; + } + \ No newline at end of file diff --git a/tests/baselines/reference/typePredicateInherit.errors.txt b/tests/baselines/reference/typePredicateInherit.errors.txt index cd5f1e6db173d..c29eaef9616f8 100644 --- a/tests/baselines/reference/typePredicateInherit.errors.txt +++ b/tests/baselines/reference/typePredicateInherit.errors.txt @@ -1,15 +1,21 @@ 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; }'. - Type 'void' is not assignable to type 'boolean'. + 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; }'. - Type 'void' is not assignable to type 'boolean'. + 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 (3 errors) ==== +==== typePredicateInherit.ts (5 errors) ==== interface A { method1(): this is { a: 1 @@ -24,7 +30,7 @@ typePredicateInherit.ts(41,3): error TS2416: Property 'method1' in type 'D' is n ~~~~~~~ !!! 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: Type 'void' is not assignable to type 'boolean'. +!!! error TS2416: Signature '(): void' must be a type predicate. method2() { } // should error ~~~~~~~ @@ -32,7 +38,11 @@ typePredicateInherit.ts(41,3): error TS2416: Property 'method1' in type 'D' is n !!! error TS2416: Type '() => void' is not assignable to type '() => boolean'. !!! error TS2416: Type 'void' is not assignable to type 'boolean'. - method3() { // should ok + 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 } } @@ -62,7 +72,7 @@ typePredicateInherit.ts(41,3): error TS2416: Property 'method1' in type 'D' is n ~~~~~~~ !!! 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: Type 'void' is not assignable to type 'boolean'. +!!! error TS2416: Signature '(): void' must be a type predicate. } method2(): this is { // should ok @@ -71,7 +81,11 @@ typePredicateInherit.ts(41,3): error TS2416: Property 'method1' in type 'D' is n return true; } - method3(): boolean { // should ok + 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; } } \ No newline at end of file diff --git a/tests/baselines/reference/typePredicateInherit.js b/tests/baselines/reference/typePredicateInherit.js index e19fa14be8915..140ab72902701 100644 --- a/tests/baselines/reference/typePredicateInherit.js +++ b/tests/baselines/reference/typePredicateInherit.js @@ -15,7 +15,7 @@ class B implements A { method2() { } // should error - method3() { // should ok + method3() { // should error return true } } @@ -50,7 +50,7 @@ class D extends C { return true; } - method3(): boolean { // should ok + method3(): boolean { // should error return true; } } diff --git a/tests/baselines/reference/typePredicateInherit.symbols b/tests/baselines/reference/typePredicateInherit.symbols index 1777be5038254..00f31ea459c23 100644 --- a/tests/baselines/reference/typePredicateInherit.symbols +++ b/tests/baselines/reference/typePredicateInherit.symbols @@ -31,7 +31,7 @@ class B implements A { method2() { } // should error >method2 : Symbol(B.method2, Decl(typePredicateInherit.ts, 10, 15)) - method3() { // should ok + method3() { // should error >method3 : Symbol(B.method3, Decl(typePredicateInherit.ts, 12, 15)) return true @@ -90,7 +90,7 @@ class D extends C { return true; } - method3(): boolean { // should ok + method3(): boolean { // should error >method3 : Symbol(D.method3, Decl(typePredicateInherit.ts, 47, 3)) return true; diff --git a/tests/baselines/reference/typePredicateInherit.types b/tests/baselines/reference/typePredicateInherit.types index 6149a44008824..0aff43c198d92 100644 --- a/tests/baselines/reference/typePredicateInherit.types +++ b/tests/baselines/reference/typePredicateInherit.types @@ -28,7 +28,7 @@ class B implements A { method2() { } // should error >method2 : () => void - method3() { // should ok + method3() { // should error >method3 : () => boolean return true @@ -92,7 +92,7 @@ class D extends C { >true : true } - method3(): boolean { // should ok + method3(): boolean { // should error >method3 : () => boolean return true; diff --git a/tests/cases/compiler/typePredicateInherit.ts b/tests/cases/compiler/typePredicateInherit.ts index 213e58b48af1b..60d40463e414c 100644 --- a/tests/cases/compiler/typePredicateInherit.ts +++ b/tests/cases/compiler/typePredicateInherit.ts @@ -12,7 +12,7 @@ class B implements A { method2() { } // should error - method3() { // should ok + method3() { // should error return true } } @@ -47,7 +47,7 @@ class D extends C { return true; } - method3(): boolean { // should ok + method3(): boolean { // should error return true; } } \ No newline at end of file From c0682969c895f7e88262c0bce360e3bcc05dcca7 Mon Sep 17 00:00:00 2001 From: Zzzen Date: Sat, 6 Apr 2024 16:48:12 +0800 Subject: [PATCH 4/5] update baseline --- tests/baselines/reference/implementArrayInterface.types | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/baselines/reference/implementArrayInterface.types b/tests/baselines/reference/implementArrayInterface.types index b3afd45a48a6f..8dfcb2f054d76 100644 --- a/tests/baselines/reference/implementArrayInterface.types +++ b/tests/baselines/reference/implementArrayInterface.types @@ -117,6 +117,7 @@ declare class MyArray implements Array { >array : T[] > : ^^^ >thisArg : any +> : ^^^ some(callbackfn: (value: T, index: number, array: T[]) => boolean, thisArg?: any): boolean; >some : (callbackfn: (value: T, index: number, array: T[]) => boolean, thisArg?: any) => boolean @@ -130,6 +131,7 @@ declare class MyArray implements Array { >array : T[] > : ^^^ >thisArg : any +> : ^^^ forEach(callbackfn: (value: T, index: number, array: T[]) => void, thisArg?: any): void; >forEach : (callbackfn: (value: T, index: number, array: T[]) => void, thisArg?: any) => void @@ -143,6 +145,7 @@ declare class MyArray implements Array { >array : T[] > : ^^^ >thisArg : any +> : ^^^ map(callbackfn: (value: T, index: number, array: T[]) => U, thisArg?: any): U[]; >map : (callbackfn: (value: T, index: number, array: T[]) => U, thisArg?: any) => U[] @@ -156,6 +159,7 @@ declare class MyArray implements Array { >array : T[] > : ^^^ >thisArg : any +> : ^^^ filter(callbackfn: (value: T, index: number, array: T[]) => boolean, thisArg?: any): T[]; >filter : (callbackfn: (value: T, index: number, array: T[]) => boolean, thisArg?: any) => T[] @@ -169,6 +173,7 @@ declare class MyArray implements Array { >array : T[] > : ^^^ >thisArg : any +> : ^^^ reduce(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue?: T): T; >reduce : { (callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue?: T): T; (callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U, initialValue: U): U; } From f889bfc59fe38e0a101759ef43e6d96b445d33b8 Mon Sep 17 00:00:00 2001 From: Zzzen Date: Sat, 6 Apr 2024 17:25:37 +0800 Subject: [PATCH 5/5] update baseline --- .../reference/typePredicateInherit.types | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tests/baselines/reference/typePredicateInherit.types b/tests/baselines/reference/typePredicateInherit.types index 0aff43c198d92..eb6a03190bfa4 100644 --- a/tests/baselines/reference/typePredicateInherit.types +++ b/tests/baselines/reference/typePredicateInherit.types @@ -4,98 +4,126 @@ interface A { method1(): this is { >method1 : () => this is { a: 1; } +> : ^^^^^^ a: 1 >a : 1 +> : ^ } method2(): boolean; >method2 : () => boolean +> : ^^^^^^ method3(): this is { >method3 : () => this is { a: 1; } +> : ^^^^^^ a: 1 >a : 1 +> : ^ }; } class B implements A { >B : B +> : ^ method1() { } // should error >method1 : () => void +> : ^^^^^^^^^^ method2() { } // should error >method2 : () => void +> : ^^^^^^^^^^ method3() { // should error >method3 : () => boolean +> : ^^^^^^^^^^^^^ return true >true : true +> : ^^^^ } } class C { >C : C +> : ^ method1(): this is { >method1 : () => this is { a: 1; } +> : ^^^^^^ a: 1 >a : 1 +> : ^ } { return true; >true : true +> : ^^^^ } method2(): this is { >method2 : () => this is { a: 1; } +> : ^^^^^^ a: 1 >a : 1 +> : ^ } { return true; >true : true +> : ^^^^ } method3(): this is { >method3 : () => this is { a: 1; } +> : ^^^^^^ a: 1 >a : 1 +> : ^ } { return true; >true : true +> : ^^^^ } } class D extends C { >D : D +> : ^ >C : C +> : ^ method1(): void { // should error >method1 : () => void +> : ^^^^^^ } method2(): this is { // should ok >method2 : () => this is { a: 1; } +> : ^^^^^^ a: 1 >a : 1 +> : ^ } { return true; >true : true +> : ^^^^ } method3(): boolean { // should error >method3 : () => boolean +> : ^^^^^^ return true; >true : true +> : ^^^^ } }