diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index e496a0b07bc..3296824123a 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -5633,6 +5633,16 @@ namespace ts { if (targetReturnType === voidType) return result; const sourceReturnType = getReturnTypeOfSignature(source); + // The follow block preserves old behavior forbidding boolean returning functions from being assignable to type guard returning functions + if (targetReturnType.flags & TypeFlags.PredicateType && (targetReturnType as PredicateType).predicate.kind === TypePredicateKind.Identifier) { + if (!(sourceReturnType.flags & TypeFlags.PredicateType)) { + if (reportErrors) { + reportError(Diagnostics.Signature_0_must_have_a_type_predicate, signatureToString(source)); + } + return Ternary.False; + } + } + return result & isRelatedTo(sourceReturnType, targetReturnType, reportErrors); } diff --git a/tests/baselines/reference/stringLiteralTypesAsTags01.errors.txt b/tests/baselines/reference/stringLiteralTypesAsTags01.errors.txt index de3b7be2612..90c8c3efdf7 100644 --- a/tests/baselines/reference/stringLiteralTypesAsTags01.errors.txt +++ b/tests/baselines/reference/stringLiteralTypesAsTags01.errors.txt @@ -1,7 +1,8 @@ +tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags01.ts(20,10): error TS2394: Overload signature is not compatible with function implementation. tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags01.ts(22,21): error TS2304: Cannot find name 'is'. -==== tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags01.ts (1 errors) ==== +==== tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags01.ts (2 errors) ==== type Kind = "A" | "B" @@ -22,6 +23,8 @@ tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags01.ts(22,21) function hasKind(entity: Entity, kind: "A"): entity is A; function hasKind(entity: Entity, kind: "B"): entity is B; function hasKind(entity: Entity, kind: Kind): entity is Entity; + ~~~~~~~ +!!! error TS2394: Overload signature is not compatible with function implementation. function hasKind(entity: Entity, kind: Kind): boolean { return kind === is; ~~ diff --git a/tests/baselines/reference/typeGuardFunctionErrors.errors.txt b/tests/baselines/reference/typeGuardFunctionErrors.errors.txt index 445738e9783..e1fdf303870 100644 --- a/tests/baselines/reference/typeGuardFunctionErrors.errors.txt +++ b/tests/baselines/reference/typeGuardFunctionErrors.errors.txt @@ -17,6 +17,8 @@ tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(75,46) Type predicate 'p1 is C' is not assignable to 'p1 is B'. Type 'C' is not assignable to type 'B'. Property 'propB' is missing in type 'C'. +tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(79,1): error TS2322: Type '(p1: any, p2: any) => boolean' is not assignable to type '(p1: any, p2: any) => p1 is A'. + Signature '(p1: any, p2: any): boolean' must have a type predicate. tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(85,1): error TS2322: Type '(p1: any, p2: any) => p2 is A' is not assignable to type '(p1: any, p2: any) => p1 is A'. Type predicate 'p2 is A' is not assignable to 'p1 is A'. Parameter 'p2' is not in the same position as parameter 'p1'. @@ -39,7 +41,7 @@ tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(133,34 tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(137,39): error TS1230: A type predicate cannot reference element 'p1' in a binding pattern. -==== tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts (32 errors) ==== +==== tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts (33 errors) ==== class A { propA: number; @@ -153,6 +155,9 @@ tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(137,39 // Boolean not assignable to type guard var assign1: (p1, p2) => p1 is A; assign1 = function(p1, p2): boolean { + ~~~~~~~ +!!! error TS2322: Type '(p1: any, p2: any) => boolean' is not assignable to type '(p1: any, p2: any) => p1 is A'. +!!! error TS2322: Signature '(p1: any, p2: any): boolean' must have a type predicate. return true; };