Skip to content

Commit 60304d8

Browse files
OliverJAshastlouisf
andcommitted
Adjust error node
Co-authored-by: Alexandre St-Louis Fortier <alexandre@unsplash.com>
1 parent f6c6b4d commit 60304d8

File tree

6 files changed

+22
-19
lines changed

6 files changed

+22
-19
lines changed

src/compiler/checker.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45798,7 +45798,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
4579845798
: exprType;
4579945799

4580045800
const effectiveExpr = expr && getEffectiveCheckNode(expr); // The effective expression for diagnostics purposes.
45801-
const errorNode = inReturnStatement && !inConditionalExpression ? node : effectiveExpr;
45801+
const errorNode = inConditionalExpression ? effectiveExpr :
45802+
inReturnStatement ? node :
45803+
isArrowFunction(node.parent) && node.parent.type !== undefined ? node.parent.type :
45804+
effectiveExpr;
4580245805

4580345806
// If the return type is not narrowable, we simply check if the return expression type is assignable to the return type.
4580445807
if (!(unwrappedReturnType.flags & (TypeFlags.IndexedAccess | TypeFlags.Conditional)) || !couldContainTypeVariables(unwrappedReturnType)) {

tests/baselines/reference/arrowFunctionErrorSpan.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ arrowFunctionErrorSpan.ts(43,5): error TS2345: Argument of type '() => void' is
2020
arrowFunctionErrorSpan.ts(52,3): error TS2345: Argument of type '(_: any) => number' is not assignable to parameter of type '() => number'.
2121
Target signature provides too few arguments. Expected 1 or more, but got 0.
2222
arrowFunctionErrorSpan.ts(55,7): error TS2355: A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value.
23-
arrowFunctionErrorSpan.ts(57,17): error TS2322: Type 'string' is not assignable to type 'number'.
23+
arrowFunctionErrorSpan.ts(57,7): error TS2322: Type 'string' is not assignable to type 'number'.
2424

2525

2626
==== arrowFunctionErrorSpan.ts (13 errors) ====
@@ -120,6 +120,6 @@ arrowFunctionErrorSpan.ts(57,17): error TS2322: Type 'string' is not assignable
120120
!!! error TS2355: A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value.
121121

122122
f((): number => '');
123-
~~
123+
~~~~~~
124124
!!! error TS2322: Type 'string' is not assignable to type 'number'.
125125

tests/baselines/reference/arrowFunctionReturnTypeErrorSpan.errors.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ arrowFunctionReturnTypeErrorSpan.ts(2,7): error TS2451: Cannot redeclare block-s
22
arrowFunctionReturnTypeErrorSpan.ts(3,3): error TS2322: Type 'string' is not assignable to type 'number'.
33
arrowFunctionReturnTypeErrorSpan.ts(6,7): error TS2451: Cannot redeclare block-scoped variable 'a'.
44
arrowFunctionReturnTypeErrorSpan.ts(7,10): error TS2304: Cannot find name 'missing'.
5-
arrowFunctionReturnTypeErrorSpan.ts(11,25): error TS2322: Type 'string' is not assignable to type 'number'.
6-
arrowFunctionReturnTypeErrorSpan.ts(14,28): error TS2322: Type 'string' is not assignable to type 'number'.
5+
arrowFunctionReturnTypeErrorSpan.ts(11,15): error TS2322: Type 'string' is not assignable to type 'number'.
6+
arrowFunctionReturnTypeErrorSpan.ts(14,15): error TS2322: Type 'string' is not assignable to type 'number'.
77
arrowFunctionReturnTypeErrorSpan.ts(16,25): error TS2304: Cannot find name 'missing'.
88

99

@@ -27,12 +27,12 @@ arrowFunctionReturnTypeErrorSpan.ts(16,25): error TS2304: Cannot find name 'miss
2727

2828
// expression body
2929
const b = (): number => "foo";
30-
~~~~~
30+
~~~~~~
3131
!!! error TS2322: Type 'string' is not assignable to type 'number'.
3232

3333
type F<T> = T;
3434
const c = (): F<number> => "foo";
35-
~~~~~
35+
~~~~~~~~~
3636
!!! error TS2322: Type 'string' is not assignable to type 'number'.
3737

3838
const d = (): number => missing;

tests/baselines/reference/conditionalTypes1.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ conditionalTypes1.ts(160,5): error TS2322: Type 'T' is not assignable to type 'Z
7373
Type 'string | number' is not assignable to type 'ZeroOf<T>'.
7474
Type 'string' is not assignable to type 'ZeroOf<T>'.
7575
conditionalTypes1.ts(263,9): error TS2403: Subsequent variable declarations must have the same type. Variable 'z' must be of type 'T1', but here has type 'Foo<T & U>'.
76-
conditionalTypes1.ts(288,43): error TS2322: Type 'T95<U>' is not assignable to type 'T94<U>'.
76+
conditionalTypes1.ts(288,33): error TS2322: Type 'T95<U>' is not assignable to type 'T94<U>'.
7777
Type 'number | boolean' is not assignable to type 'T94<U>'.
7878
Type 'number' is not assignable to type 'T94<U>'.
7979
Type 'boolean' is not assignable to type 'true'.
@@ -465,7 +465,7 @@ conditionalTypes1.ts(288,43): error TS2322: Type 'T95<U>' is not assignable to t
465465
type T95<T> = T extends string ? boolean : number;
466466
const f44 = <U>(value: T94<U>): T95<U> => value;
467467
const f45 = <U>(value: T95<U>): T94<U> => value; // Error
468-
~~~~~
468+
~~~~~~
469469
!!! error TS2322: Type 'T95<U>' is not assignable to type 'T94<U>'.
470470
!!! error TS2322: Type 'number | boolean' is not assignable to type 'T94<U>'.
471471
!!! error TS2322: Type 'number' is not assignable to type 'T94<U>'.

tests/baselines/reference/mappedTypeConstraints2.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ mappedTypeConstraints2.ts(16,11): error TS2322: Type 'Mapped3<K>[Uppercase<K>]'
66
mappedTypeConstraints2.ts(42,7): error TS2322: Type 'Mapped6<K>[keyof Mapped6<K>]' is not assignable to type '`_${string}`'.
77
Type 'Mapped6<K>[string] | Mapped6<K>[number] | Mapped6<K>[symbol]' is not assignable to type '`_${string}`'.
88
Type 'Mapped6<K>[string]' is not assignable to type '`_${string}`'.
9-
mappedTypeConstraints2.ts(51,57): error TS2322: Type 'Foo<T>[`get${T}`]' is not assignable to type 'T'.
9+
mappedTypeConstraints2.ts(51,52): error TS2322: Type 'Foo<T>[`get${T}`]' is not assignable to type 'T'.
1010
'T' could be instantiated with an arbitrary type which could be unrelated to 'Foo<T>[`get${T}`]'.
1111
mappedTypeConstraints2.ts(82,9): error TS2322: Type 'ObjectWithUnderscoredKeys<K>[`_${K}`]' is not assignable to type 'true'.
1212
Type 'ObjectWithUnderscoredKeys<K>[`_${string}`]' is not assignable to type 'true'.
@@ -75,7 +75,7 @@ mappedTypeConstraints2.ts(82,9): error TS2322: Type 'ObjectWithUnderscoredKeys<K
7575
};
7676

7777
const get = <T extends string>(t: T, foo: Foo<T>): T => foo[`get${t}`]; // Type 'Foo<T>[`get${T}`]' is not assignable to type 'T'
78-
~~~~~~~~~~~~~~
78+
~
7979
!!! error TS2322: Type 'Foo<T>[`get${T}`]' is not assignable to type 'T'.
8080
!!! error TS2322: 'T' could be instantiated with an arbitrary type which could be unrelated to 'Foo<T>[`get${T}`]'.
8181

tests/baselines/reference/typeSatisfaction_errorLocations1.errors.txt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ typeSatisfaction_errorLocations1.ts(36,9): error TS2345: Argument of type 'strin
2626
typeSatisfaction_errorLocations1.ts(39,3): error TS2322: Type 'string' is not assignable to type 'number'.
2727
typeSatisfaction_errorLocations1.ts(43,3): error TS2322: Type 'string' is not assignable to type 'number'.
2828
typeSatisfaction_errorLocations1.ts(43,16): error TS1360: Type 'string' does not satisfy the expected type 'number'.
29-
typeSatisfaction_errorLocations1.ts(46,22): error TS2741: Property 'a' is missing in type '{}' but required in type '{ a: true; }'.
29+
typeSatisfaction_errorLocations1.ts(46,6): error TS2741: Property 'a' is missing in type '{}' but required in type '{ a: true; }'.
3030
typeSatisfaction_errorLocations1.ts(47,24): error TS2322: Type 'number' is not assignable to type 'true'.
31-
typeSatisfaction_errorLocations1.ts(48,21): error TS2322: Type '{ a: number; }' is not assignable to type '{ a: true; }'.
31+
typeSatisfaction_errorLocations1.ts(48,6): error TS2322: Type '{ a: number; }' is not assignable to type '{ a: true; }'.
3232
Types of property 'a' are incompatible.
3333
Type 'number' is not assignable to type 'true'.
34-
typeSatisfaction_errorLocations1.ts(50,23): error TS2741: Property 'a' is missing in type '{}' but required in type '{ a: true; }'.
35-
typeSatisfaction_errorLocations1.ts(51,24): error TS2741: Property 'a' is missing in type '{}' but required in type '{ a: true; }'.
34+
typeSatisfaction_errorLocations1.ts(50,6): error TS2741: Property 'a' is missing in type '{}' but required in type '{ a: true; }'.
35+
typeSatisfaction_errorLocations1.ts(51,6): error TS2741: Property 'a' is missing in type '{}' but required in type '{ a: true; }'.
3636

3737

3838
==== typeSatisfaction_errorLocations1.ts (24 errors) ====
@@ -133,25 +133,25 @@ typeSatisfaction_errorLocations1.ts(51,24): error TS2741: Property 'a' is missin
133133
}
134134

135135
((): { a: true } => ({}) satisfies unknown)();
136-
~~
136+
~~~~~~~~~~~
137137
!!! error TS2741: Property 'a' is missing in type '{}' but required in type '{ a: true; }'.
138138
!!! related TS2728 typeSatisfaction_errorLocations1.ts:46:8: 'a' is declared here.
139139
((): { a: true } => ({ a: 1 }) satisfies unknown)();
140140
~
141141
!!! error TS2322: Type 'number' is not assignable to type 'true'.
142142
!!! related TS6500 typeSatisfaction_errorLocations1.ts:47:8: The expected type comes from property 'a' which is declared here on type '{ a: true; }'
143143
((): { a: true } => obj1 satisfies unknown)();
144-
~~~~
144+
~~~~~~~~~~~
145145
!!! error TS2322: Type '{ a: number; }' is not assignable to type '{ a: true; }'.
146146
!!! error TS2322: Types of property 'a' are incompatible.
147147
!!! error TS2322: Type 'number' is not assignable to type 'true'.
148148

149149
((): { a: true } => (({}) satisfies unknown) satisfies unknown)();
150-
~~
150+
~~~~~~~~~~~
151151
!!! error TS2741: Property 'a' is missing in type '{}' but required in type '{ a: true; }'.
152152
!!! related TS2728 typeSatisfaction_errorLocations1.ts:50:8: 'a' is declared here.
153153
((): { a: true } => ((({}) satisfies unknown)) satisfies unknown)();
154-
~~
154+
~~~~~~~~~~~
155155
!!! error TS2741: Property 'a' is missing in type '{}' but required in type '{ a: true; }'.
156156
!!! related TS2728 typeSatisfaction_errorLocations1.ts:51:8: 'a' is declared here.
157157

0 commit comments

Comments
 (0)