Skip to content

Commit 6b4a3b2

Browse files
authored
Merge pull request #25500 from mattmccutchen/issue-25498
Fix crash in elaborateElementwise when the target property is known but it doesn't have a declaration (e.g., in a mapped type).
2 parents b5f5513 + d5fd17b commit 6b4a3b2

File tree

6 files changed

+53
-3
lines changed

6 files changed

+53
-3
lines changed

src/compiler/checker.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10367,9 +10367,9 @@ namespace ts {
1036710367
}
1036810368
}
1036910369

10370-
if (!issuedElaboration && (length(targetProp && targetProp.declarations) || length(target.symbol && target.symbol.declarations))) {
10370+
if (!issuedElaboration && (targetProp && length(targetProp.declarations) || target.symbol && length(target.symbol.declarations))) {
1037110371
addRelatedInfo(reportedDiag, createDiagnosticForNode(
10372-
targetProp ? targetProp.declarations[0] : target.symbol.declarations[0],
10372+
targetProp && length(targetProp.declarations) ? targetProp.declarations[0] : target.symbol.declarations[0],
1037310373
Diagnostics.The_expected_type_comes_from_property_0_which_is_declared_here_on_type_1,
1037410374
propertyName && !(nameType.flags & TypeFlags.UniqueESSymbol) ? unescapeLeadingUnderscores(propertyName) : typeToString(nameType),
1037510375
typeToString(target)

tests/baselines/reference/errorElaboration.errors.txt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ tests/cases/compiler/errorElaboration.ts(12,5): error TS2345: Argument of type '
22
Type 'Container<Ref<string>>' is not assignable to type 'Container<Ref<number>>'.
33
Type 'Ref<string>' is not assignable to type 'Ref<number>'.
44
Type 'string' is not assignable to type 'number'.
5+
tests/cases/compiler/errorElaboration.ts(17,11): error TS2322: Type '"bar"' is not assignable to type '"foo"'.
56

67

7-
==== tests/cases/compiler/errorElaboration.ts (1 errors) ====
8+
==== tests/cases/compiler/errorElaboration.ts (2 errors) ====
89
// Repro for #5712
910

1011
interface Ref<T> {
@@ -22,4 +23,13 @@ tests/cases/compiler/errorElaboration.ts(12,5): error TS2345: Argument of type '
2223
!!! error TS2345: Type 'Container<Ref<string>>' is not assignable to type 'Container<Ref<number>>'.
2324
!!! error TS2345: Type 'Ref<string>' is not assignable to type 'Ref<number>'.
2425
!!! error TS2345: Type 'string' is not assignable to type 'number'.
26+
27+
// Repro for #25498
28+
29+
function test(): {[A in "foo"]: A} {
30+
return {foo: "bar"};
31+
~~~
32+
!!! error TS2322: Type '"bar"' is not assignable to type '"foo"'.
33+
!!! related TS6500 tests/cases/compiler/errorElaboration.ts:16:18: The expected type comes from property 'foo' which is declared here on type '{ foo: "foo"; }'
34+
}
2535

tests/baselines/reference/errorElaboration.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,19 @@ interface Container<T> {
1111
declare function foo(x: () => Container<Ref<number>>): void;
1212
let a: () => Container<Ref<string>>;
1313
foo(a);
14+
15+
// Repro for #25498
16+
17+
function test(): {[A in "foo"]: A} {
18+
return {foo: "bar"};
19+
}
1420

1521

1622
//// [errorElaboration.js]
1723
// Repro for #5712
1824
var a;
1925
foo(a);
26+
// Repro for #25498
27+
function test() {
28+
return { foo: "bar" };
29+
}

tests/baselines/reference/errorElaboration.symbols

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,14 @@ foo(a);
3838
>foo : Symbol(foo, Decl(errorElaboration.ts, 8, 1))
3939
>a : Symbol(a, Decl(errorElaboration.ts, 10, 3))
4040

41+
// Repro for #25498
42+
43+
function test(): {[A in "foo"]: A} {
44+
>test : Symbol(test, Decl(errorElaboration.ts, 11, 7))
45+
>A : Symbol(A, Decl(errorElaboration.ts, 15, 19))
46+
>A : Symbol(A, Decl(errorElaboration.ts, 15, 19))
47+
48+
return {foo: "bar"};
49+
>foo : Symbol(foo, Decl(errorElaboration.ts, 16, 10))
50+
}
51+

tests/baselines/reference/errorElaboration.types

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,16 @@ foo(a);
3939
>foo : (x: () => Container<Ref<number>>) => void
4040
>a : () => Container<Ref<string>>
4141

42+
// Repro for #25498
43+
44+
function test(): {[A in "foo"]: A} {
45+
>test : () => { foo: "foo"; }
46+
>A : A
47+
>A : A
48+
49+
return {foo: "bar"};
50+
>{foo: "bar"} : { foo: "bar"; }
51+
>foo : "bar"
52+
>"bar" : "bar"
53+
}
54+

tests/cases/compiler/errorElaboration.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,9 @@ interface Container<T> {
1010
declare function foo(x: () => Container<Ref<number>>): void;
1111
let a: () => Container<Ref<string>>;
1212
foo(a);
13+
14+
// Repro for #25498
15+
16+
function test(): {[A in "foo"]: A} {
17+
return {foo: "bar"};
18+
}

0 commit comments

Comments
 (0)