Skip to content

Commit 0228ec3

Browse files
committed
preserve some old behavior at @DanielRosenwassers request
1 parent 4594301 commit 0228ec3

3 files changed

Lines changed: 20 additions & 2 deletions

File tree

src/compiler/checker.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5633,6 +5633,16 @@ namespace ts {
56335633
if (targetReturnType === voidType) return result;
56345634
const sourceReturnType = getReturnTypeOfSignature(source);
56355635

5636+
// The follow block preserves old behavior forbidding boolean returning functions from being assignable to type guard returning functions
5637+
if (targetReturnType.flags & TypeFlags.PredicateType && (targetReturnType as PredicateType).predicate.kind === TypePredicateKind.Identifier) {
5638+
if (!(sourceReturnType.flags & TypeFlags.PredicateType)) {
5639+
if (reportErrors) {
5640+
reportError(Diagnostics.Signature_0_must_have_a_type_predicate, signatureToString(source));
5641+
}
5642+
return Ternary.False;
5643+
}
5644+
}
5645+
56365646
return result & isRelatedTo(sourceReturnType, targetReturnType, reportErrors);
56375647
}
56385648

tests/baselines/reference/stringLiteralTypesAsTags01.errors.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags01.ts(20,10): error TS2394: Overload signature is not compatible with function implementation.
12
tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags01.ts(22,21): error TS2304: Cannot find name 'is'.
23

34

4-
==== tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags01.ts (1 errors) ====
5+
==== tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags01.ts (2 errors) ====
56

67
type Kind = "A" | "B"
78

@@ -22,6 +23,8 @@ tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags01.ts(22,21)
2223
function hasKind(entity: Entity, kind: "A"): entity is A;
2324
function hasKind(entity: Entity, kind: "B"): entity is B;
2425
function hasKind(entity: Entity, kind: Kind): entity is Entity;
26+
~~~~~~~
27+
!!! error TS2394: Overload signature is not compatible with function implementation.
2528
function hasKind(entity: Entity, kind: Kind): boolean {
2629
return kind === is;
2730
~~

tests/baselines/reference/typeGuardFunctionErrors.errors.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(75,46)
1717
Type predicate 'p1 is C' is not assignable to 'p1 is B'.
1818
Type 'C' is not assignable to type 'B'.
1919
Property 'propB' is missing in type 'C'.
20+
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'.
21+
Signature '(p1: any, p2: any): boolean' must have a type predicate.
2022
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'.
2123
Type predicate 'p2 is A' is not assignable to 'p1 is A'.
2224
Parameter 'p2' is not in the same position as parameter 'p1'.
@@ -39,7 +41,7 @@ tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(133,34
3941
tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(137,39): error TS1230: A type predicate cannot reference element 'p1' in a binding pattern.
4042

4143

42-
==== tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts (32 errors) ====
44+
==== tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts (33 errors) ====
4345

4446
class A {
4547
propA: number;
@@ -153,6 +155,9 @@ tests/cases/conformance/expressions/typeGuards/typeGuardFunctionErrors.ts(137,39
153155
// Boolean not assignable to type guard
154156
var assign1: (p1, p2) => p1 is A;
155157
assign1 = function(p1, p2): boolean {
158+
~~~~~~~
159+
!!! error TS2322: Type '(p1: any, p2: any) => boolean' is not assignable to type '(p1: any, p2: any) => p1 is A'.
160+
!!! error TS2322: Signature '(p1: any, p2: any): boolean' must have a type predicate.
156161
return true;
157162
};
158163

0 commit comments

Comments
 (0)