Skip to content

Commit 0ca1973

Browse files
authored
Fixed widening errors locations in union-normalized object literal types (#58157)
1 parent 61ffce0 commit 0ca1973

5 files changed

+207
-11
lines changed

src/compiler/checker.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25281,27 +25281,28 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
2528125281
}
2528225282
else {
2528325283
for (const t of (type as UnionType).types) {
25284-
if (reportWideningErrorsInType(t)) {
25285-
errorReported = true;
25286-
}
25284+
errorReported ||= reportWideningErrorsInType(t);
2528725285
}
2528825286
}
2528925287
}
25290-
if (isArrayOrTupleType(type)) {
25288+
else if (isArrayOrTupleType(type)) {
2529125289
for (const t of getTypeArguments(type)) {
25292-
if (reportWideningErrorsInType(t)) {
25293-
errorReported = true;
25294-
}
25290+
errorReported ||= reportWideningErrorsInType(t);
2529525291
}
2529625292
}
25297-
if (isObjectLiteralType(type)) {
25293+
else if (isObjectLiteralType(type)) {
2529825294
for (const p of getPropertiesOfObjectType(type)) {
2529925295
const t = getTypeOfSymbol(p);
2530025296
if (getObjectFlags(t) & ObjectFlags.ContainsWideningType) {
25301-
if (!reportWideningErrorsInType(t)) {
25302-
error(p.valueDeclaration, Diagnostics.Object_literal_s_property_0_implicitly_has_an_1_type, symbolToString(p), typeToString(getWidenedType(t)));
25297+
errorReported = reportWideningErrorsInType(t);
25298+
if (!errorReported) {
25299+
// we need to account for property types coming from object literal type normalization in unions
25300+
const valueDeclaration = p.declarations?.find(d => d.symbol.valueDeclaration?.parent === type.symbol.valueDeclaration);
25301+
if (valueDeclaration) {
25302+
error(valueDeclaration, Diagnostics.Object_literal_s_property_0_implicitly_has_an_1_type, symbolToString(p), typeToString(getWidenedType(t)));
25303+
errorReported = true;
25304+
}
2530325305
}
25304-
errorReported = true;
2530525306
}
2530625307
}
2530725308
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
noImplicitAnyUnionNormalizedObjectLiteral1.ts(16,3): error TS7018: Object literal's property 'p' implicitly has an 'any' type.
2+
noImplicitAnyUnionNormalizedObjectLiteral1.ts(17,3): error TS7018: Object literal's property 's' implicitly has an 'any' type.
3+
4+
5+
==== noImplicitAnyUnionNormalizedObjectLiteral1.ts (2 errors) ====
6+
// https://github.com/microsoft/TypeScript/issues/58150
7+
8+
function doSthWithParams(params: unknown) {
9+
if (typeof params !== "object") {
10+
return {};
11+
}
12+
13+
return {
14+
c: "foo",
15+
p: "bar",
16+
s: "baz",
17+
};
18+
}
19+
20+
const bar = {
21+
p: null,
22+
~~~~~~~
23+
!!! error TS7018: Object literal's property 'p' implicitly has an 'any' type.
24+
s: null,
25+
~~~~~~~
26+
!!! error TS7018: Object literal's property 's' implicitly has an 'any' type.
27+
...doSthWithParams({
28+
p: "hello",
29+
s: "world",
30+
}),
31+
};
32+
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//// [tests/cases/compiler/noImplicitAnyUnionNormalizedObjectLiteral1.ts] ////
2+
3+
=== noImplicitAnyUnionNormalizedObjectLiteral1.ts ===
4+
// https://github.com/microsoft/TypeScript/issues/58150
5+
6+
function doSthWithParams(params: unknown) {
7+
>doSthWithParams : Symbol(doSthWithParams, Decl(noImplicitAnyUnionNormalizedObjectLiteral1.ts, 0, 0))
8+
>params : Symbol(params, Decl(noImplicitAnyUnionNormalizedObjectLiteral1.ts, 2, 25))
9+
10+
if (typeof params !== "object") {
11+
>params : Symbol(params, Decl(noImplicitAnyUnionNormalizedObjectLiteral1.ts, 2, 25))
12+
13+
return {};
14+
}
15+
16+
return {
17+
c: "foo",
18+
>c : Symbol(c, Decl(noImplicitAnyUnionNormalizedObjectLiteral1.ts, 7, 10))
19+
20+
p: "bar",
21+
>p : Symbol(p, Decl(noImplicitAnyUnionNormalizedObjectLiteral1.ts, 8, 13))
22+
23+
s: "baz",
24+
>s : Symbol(s, Decl(noImplicitAnyUnionNormalizedObjectLiteral1.ts, 9, 13))
25+
26+
};
27+
}
28+
29+
const bar = {
30+
>bar : Symbol(bar, Decl(noImplicitAnyUnionNormalizedObjectLiteral1.ts, 14, 5))
31+
32+
p: null,
33+
>p : Symbol(p, Decl(noImplicitAnyUnionNormalizedObjectLiteral1.ts, 14, 13))
34+
35+
s: null,
36+
>s : Symbol(s, Decl(noImplicitAnyUnionNormalizedObjectLiteral1.ts, 15, 10))
37+
38+
...doSthWithParams({
39+
>doSthWithParams : Symbol(doSthWithParams, Decl(noImplicitAnyUnionNormalizedObjectLiteral1.ts, 0, 0))
40+
41+
p: "hello",
42+
>p : Symbol(p, Decl(noImplicitAnyUnionNormalizedObjectLiteral1.ts, 17, 22))
43+
44+
s: "world",
45+
>s : Symbol(s, Decl(noImplicitAnyUnionNormalizedObjectLiteral1.ts, 18, 15))
46+
47+
}),
48+
};
49+
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
//// [tests/cases/compiler/noImplicitAnyUnionNormalizedObjectLiteral1.ts] ////
2+
3+
=== noImplicitAnyUnionNormalizedObjectLiteral1.ts ===
4+
// https://github.com/microsoft/TypeScript/issues/58150
5+
6+
function doSthWithParams(params: unknown) {
7+
>doSthWithParams : (params: unknown) => { c?: undefined; p?: undefined; s?: undefined; } | { c: string; p: string; s: string; }
8+
> : ^ ^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
9+
>params : unknown
10+
> : ^^^^^^^
11+
12+
if (typeof params !== "object") {
13+
>typeof params !== "object" : boolean
14+
> : ^^^^^^^
15+
>typeof params : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
16+
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
17+
>params : unknown
18+
> : ^^^^^^^
19+
>"object" : "object"
20+
> : ^^^^^^^^
21+
22+
return {};
23+
>{} : {}
24+
> : ^^
25+
}
26+
27+
return {
28+
>{ c: "foo", p: "bar", s: "baz", } : { c: string; p: string; s: string; }
29+
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
30+
31+
c: "foo",
32+
>c : string
33+
> : ^^^^^^
34+
>"foo" : "foo"
35+
> : ^^^^^
36+
37+
p: "bar",
38+
>p : string
39+
> : ^^^^^^
40+
>"bar" : "bar"
41+
> : ^^^^^
42+
43+
s: "baz",
44+
>s : string
45+
> : ^^^^^^
46+
>"baz" : "baz"
47+
> : ^^^^^
48+
49+
};
50+
}
51+
52+
const bar = {
53+
>bar : { c?: undefined; p: any; s: any; } | { c: string; p: string; s: string; }
54+
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
55+
>{ p: null, s: null, ...doSthWithParams({ p: "hello", s: "world", }),} : { c?: undefined; p: null; s: null; } | { c: string; p: string; s: string; }
56+
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
57+
58+
p: null,
59+
>p : null
60+
> : ^^^^
61+
62+
s: null,
63+
>s : null
64+
> : ^^^^
65+
66+
...doSthWithParams({
67+
>doSthWithParams({ p: "hello", s: "world", }) : { c?: undefined; p?: undefined; s?: undefined; } | { c: string; p: string; s: string; }
68+
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
69+
>doSthWithParams : (params: unknown) => { c?: undefined; p?: undefined; s?: undefined; } | { c: string; p: string; s: string; }
70+
> : ^ ^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
71+
>{ p: "hello", s: "world", } : { p: string; s: string; }
72+
> : ^^^^^^^^^^^^^^^^^^^^^^^^^
73+
74+
p: "hello",
75+
>p : string
76+
> : ^^^^^^
77+
>"hello" : "hello"
78+
> : ^^^^^^^
79+
80+
s: "world",
81+
>s : string
82+
> : ^^^^^^
83+
>"world" : "world"
84+
> : ^^^^^^^
85+
86+
}),
87+
};
88+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// @strictNullChecks: false
2+
// @noImplicitAny: true
3+
// @noEmit: true
4+
5+
// https://github.com/microsoft/TypeScript/issues/58150
6+
7+
function doSthWithParams(params: unknown) {
8+
if (typeof params !== "object") {
9+
return {};
10+
}
11+
12+
return {
13+
c: "foo",
14+
p: "bar",
15+
s: "baz",
16+
};
17+
}
18+
19+
const bar = {
20+
p: null,
21+
s: null,
22+
...doSthWithParams({
23+
p: "hello",
24+
s: "world",
25+
}),
26+
};

0 commit comments

Comments
 (0)