Skip to content

Commit d5af89c

Browse files
authored
Contextual typing checks property assignments for type annotation (#43598)
Property assignments can have a type annotation in JS. This PR adds a check for it in contextual typing. Fixes #43379
1 parent b9c1e98 commit d5af89c

File tree

4 files changed

+71
-0
lines changed

4 files changed

+71
-0
lines changed

src/compiler/checker.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25218,6 +25218,10 @@ namespace ts {
2521825218

2521925219
function getContextualTypeForObjectLiteralElement(element: ObjectLiteralElementLike, contextFlags?: ContextFlags) {
2522025220
const objectLiteral = <ObjectLiteralExpression>element.parent;
25221+
const propertyAssignmentType = isPropertyAssignment(element) && getContextualTypeForVariableLikeDeclaration(element);
25222+
if (propertyAssignmentType) {
25223+
return propertyAssignmentType;
25224+
}
2522125225
const type = getApparentTypeOfContextualType(objectLiteral, contextFlags);
2522225226
if (type) {
2522325227
if (hasBindableName(element)) {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
=== tests/cases/conformance/jsdoc/typeTagOnPropertyAssignment.js ===
2+
const o = {
3+
>o : Symbol(o, Decl(typeTagOnPropertyAssignment.js, 0, 5))
4+
5+
/**
6+
* @type {"a"}
7+
*/
8+
a: "a",
9+
>a : Symbol(a, Decl(typeTagOnPropertyAssignment.js, 0, 11))
10+
11+
/** @type {() => 'b'} */
12+
n: () => 'b'
13+
>n : Symbol(n, Decl(typeTagOnPropertyAssignment.js, 4, 11))
14+
15+
};
16+
o.a
17+
>o.a : Symbol(a, Decl(typeTagOnPropertyAssignment.js, 0, 11))
18+
>o : Symbol(o, Decl(typeTagOnPropertyAssignment.js, 0, 5))
19+
>a : Symbol(a, Decl(typeTagOnPropertyAssignment.js, 0, 11))
20+
21+
o.n
22+
>o.n : Symbol(n, Decl(typeTagOnPropertyAssignment.js, 4, 11))
23+
>o : Symbol(o, Decl(typeTagOnPropertyAssignment.js, 0, 5))
24+
>n : Symbol(n, Decl(typeTagOnPropertyAssignment.js, 4, 11))
25+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
=== tests/cases/conformance/jsdoc/typeTagOnPropertyAssignment.js ===
2+
const o = {
3+
>o : { a: "a"; n: () => 'b'; }
4+
>{ /** * @type {"a"} */ a: "a", /** @type {() => 'b'} */ n: () => 'b'} : { a: "a"; n: () => 'b'; }
5+
6+
/**
7+
* @type {"a"}
8+
*/
9+
a: "a",
10+
>a : "a"
11+
>"a" : "a"
12+
13+
/** @type {() => 'b'} */
14+
n: () => 'b'
15+
>n : () => 'b'
16+
>() => 'b' : () => 'b'
17+
>'b' : "b"
18+
19+
};
20+
o.a
21+
>o.a : "a"
22+
>o : { a: "a"; n: () => "b"; }
23+
>a : "a"
24+
25+
o.n
26+
>o.n : () => "b"
27+
>o : { a: "a"; n: () => "b"; }
28+
>n : () => "b"
29+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// @noEmit: true
2+
// @checkJs: true
3+
// @filename: typeTagOnPropertyAssignment.js
4+
const o = {
5+
/**
6+
* @type {"a"}
7+
*/
8+
a: "a",
9+
/** @type {() => 'b'} */
10+
n: () => 'b'
11+
};
12+
o.a
13+
o.n

0 commit comments

Comments
 (0)