Skip to content

Commit

Permalink
Adjust error node
Browse files Browse the repository at this point in the history
Co-authored-by: Alexandre St-Louis Fortier <[email protected]>
  • Loading branch information
OliverJAsh and astlouisf committed Dec 18, 2024
1 parent f6c6b4d commit 60304d8
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 19 deletions.
5 changes: 4 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45798,7 +45798,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
: exprType;

const effectiveExpr = expr && getEffectiveCheckNode(expr); // The effective expression for diagnostics purposes.
const errorNode = inReturnStatement && !inConditionalExpression ? node : effectiveExpr;
const errorNode = inConditionalExpression ? effectiveExpr :
inReturnStatement ? node :
isArrowFunction(node.parent) && node.parent.type !== undefined ? node.parent.type :
effectiveExpr;

// If the return type is not narrowable, we simply check if the return expression type is assignable to the return type.
if (!(unwrappedReturnType.flags & (TypeFlags.IndexedAccess | TypeFlags.Conditional)) || !couldContainTypeVariables(unwrappedReturnType)) {
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/arrowFunctionErrorSpan.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ arrowFunctionErrorSpan.ts(43,5): error TS2345: Argument of type '() => void' is
arrowFunctionErrorSpan.ts(52,3): error TS2345: Argument of type '(_: any) => number' is not assignable to parameter of type '() => number'.
Target signature provides too few arguments. Expected 1 or more, but got 0.
arrowFunctionErrorSpan.ts(55,7): error TS2355: A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value.
arrowFunctionErrorSpan.ts(57,17): error TS2322: Type 'string' is not assignable to type 'number'.
arrowFunctionErrorSpan.ts(57,7): error TS2322: Type 'string' is not assignable to type 'number'.


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

f((): number => '');
~~
~~~~~~
!!! error TS2322: Type 'string' is not assignable to type 'number'.

Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ arrowFunctionReturnTypeErrorSpan.ts(2,7): error TS2451: Cannot redeclare block-s
arrowFunctionReturnTypeErrorSpan.ts(3,3): error TS2322: Type 'string' is not assignable to type 'number'.
arrowFunctionReturnTypeErrorSpan.ts(6,7): error TS2451: Cannot redeclare block-scoped variable 'a'.
arrowFunctionReturnTypeErrorSpan.ts(7,10): error TS2304: Cannot find name 'missing'.
arrowFunctionReturnTypeErrorSpan.ts(11,25): error TS2322: Type 'string' is not assignable to type 'number'.
arrowFunctionReturnTypeErrorSpan.ts(14,28): error TS2322: Type 'string' is not assignable to type 'number'.
arrowFunctionReturnTypeErrorSpan.ts(11,15): error TS2322: Type 'string' is not assignable to type 'number'.
arrowFunctionReturnTypeErrorSpan.ts(14,15): error TS2322: Type 'string' is not assignable to type 'number'.
arrowFunctionReturnTypeErrorSpan.ts(16,25): error TS2304: Cannot find name 'missing'.


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

// expression body
const b = (): number => "foo";
~~~~~
~~~~~~
!!! error TS2322: Type 'string' is not assignable to type 'number'.

type F<T> = T;
const c = (): F<number> => "foo";
~~~~~
~~~~~~~~~
!!! error TS2322: Type 'string' is not assignable to type 'number'.

const d = (): number => missing;
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/conditionalTypes1.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ conditionalTypes1.ts(160,5): error TS2322: Type 'T' is not assignable to type 'Z
Type 'string | number' is not assignable to type 'ZeroOf<T>'.
Type 'string' is not assignable to type 'ZeroOf<T>'.
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>'.
conditionalTypes1.ts(288,43): error TS2322: Type 'T95<U>' is not assignable to type 'T94<U>'.
conditionalTypes1.ts(288,33): error TS2322: Type 'T95<U>' is not assignable to type 'T94<U>'.
Type 'number | boolean' is not assignable to type 'T94<U>'.
Type 'number' is not assignable to type 'T94<U>'.
Type 'boolean' is not assignable to type 'true'.
Expand Down Expand Up @@ -465,7 +465,7 @@ conditionalTypes1.ts(288,43): error TS2322: Type 'T95<U>' is not assignable to t
type T95<T> = T extends string ? boolean : number;
const f44 = <U>(value: T94<U>): T95<U> => value;
const f45 = <U>(value: T95<U>): T94<U> => value; // Error
~~~~~
~~~~~~
!!! error TS2322: Type 'T95<U>' is not assignable to type 'T94<U>'.
!!! error TS2322: Type 'number | boolean' is not assignable to type 'T94<U>'.
!!! error TS2322: Type 'number' is not assignable to type 'T94<U>'.
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/mappedTypeConstraints2.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mappedTypeConstraints2.ts(16,11): error TS2322: Type 'Mapped3<K>[Uppercase<K>]'
mappedTypeConstraints2.ts(42,7): error TS2322: Type 'Mapped6<K>[keyof Mapped6<K>]' is not assignable to type '`_${string}`'.
Type 'Mapped6<K>[string] | Mapped6<K>[number] | Mapped6<K>[symbol]' is not assignable to type '`_${string}`'.
Type 'Mapped6<K>[string]' is not assignable to type '`_${string}`'.
mappedTypeConstraints2.ts(51,57): error TS2322: Type 'Foo<T>[`get${T}`]' is not assignable to type 'T'.
mappedTypeConstraints2.ts(51,52): error TS2322: Type 'Foo<T>[`get${T}`]' is not assignable to type 'T'.
'T' could be instantiated with an arbitrary type which could be unrelated to 'Foo<T>[`get${T}`]'.
mappedTypeConstraints2.ts(82,9): error TS2322: Type 'ObjectWithUnderscoredKeys<K>[`_${K}`]' is not assignable to type 'true'.
Type 'ObjectWithUnderscoredKeys<K>[`_${string}`]' is not assignable to type 'true'.
Expand Down Expand Up @@ -75,7 +75,7 @@ mappedTypeConstraints2.ts(82,9): error TS2322: Type 'ObjectWithUnderscoredKeys<K
};

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'
~~~~~~~~~~~~~~
~
!!! error TS2322: Type 'Foo<T>[`get${T}`]' is not assignable to type 'T'.
!!! error TS2322: 'T' could be instantiated with an arbitrary type which could be unrelated to 'Foo<T>[`get${T}`]'.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ typeSatisfaction_errorLocations1.ts(36,9): error TS2345: Argument of type 'strin
typeSatisfaction_errorLocations1.ts(39,3): error TS2322: Type 'string' is not assignable to type 'number'.
typeSatisfaction_errorLocations1.ts(43,3): error TS2322: Type 'string' is not assignable to type 'number'.
typeSatisfaction_errorLocations1.ts(43,16): error TS1360: Type 'string' does not satisfy the expected type 'number'.
typeSatisfaction_errorLocations1.ts(46,22): error TS2741: Property 'a' is missing in type '{}' but required in type '{ a: true; }'.
typeSatisfaction_errorLocations1.ts(46,6): error TS2741: Property 'a' is missing in type '{}' but required in type '{ a: true; }'.
typeSatisfaction_errorLocations1.ts(47,24): error TS2322: Type 'number' is not assignable to type 'true'.
typeSatisfaction_errorLocations1.ts(48,21): error TS2322: Type '{ a: number; }' is not assignable to type '{ a: true; }'.
typeSatisfaction_errorLocations1.ts(48,6): error TS2322: Type '{ a: number; }' is not assignable to type '{ a: true; }'.
Types of property 'a' are incompatible.
Type 'number' is not assignable to type 'true'.
typeSatisfaction_errorLocations1.ts(50,23): error TS2741: Property 'a' is missing in type '{}' but required in type '{ a: true; }'.
typeSatisfaction_errorLocations1.ts(51,24): error TS2741: Property 'a' is missing in type '{}' but required in type '{ a: true; }'.
typeSatisfaction_errorLocations1.ts(50,6): error TS2741: Property 'a' is missing in type '{}' but required in type '{ a: true; }'.
typeSatisfaction_errorLocations1.ts(51,6): error TS2741: Property 'a' is missing in type '{}' but required in type '{ a: true; }'.


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

((): { a: true } => ({}) satisfies unknown)();
~~
~~~~~~~~~~~
!!! error TS2741: Property 'a' is missing in type '{}' but required in type '{ a: true; }'.
!!! related TS2728 typeSatisfaction_errorLocations1.ts:46:8: 'a' is declared here.
((): { a: true } => ({ a: 1 }) satisfies unknown)();
~
!!! error TS2322: Type 'number' is not assignable to type 'true'.
!!! related TS6500 typeSatisfaction_errorLocations1.ts:47:8: The expected type comes from property 'a' which is declared here on type '{ a: true; }'
((): { a: true } => obj1 satisfies unknown)();
~~~~
~~~~~~~~~~~
!!! error TS2322: Type '{ a: number; }' is not assignable to type '{ a: true; }'.
!!! error TS2322: Types of property 'a' are incompatible.
!!! error TS2322: Type 'number' is not assignable to type 'true'.

((): { a: true } => (({}) satisfies unknown) satisfies unknown)();
~~
~~~~~~~~~~~
!!! error TS2741: Property 'a' is missing in type '{}' but required in type '{ a: true; }'.
!!! related TS2728 typeSatisfaction_errorLocations1.ts:50:8: 'a' is declared here.
((): { a: true } => ((({}) satisfies unknown)) satisfies unknown)();
~~
~~~~~~~~~~~
!!! error TS2741: Property 'a' is missing in type '{}' but required in type '{ a: true; }'.
!!! related TS2728 typeSatisfaction_errorLocations1.ts:51:8: 'a' is declared here.

0 comments on commit 60304d8

Please sign in to comment.