Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d5af89c

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

4 files changed

+71Lines changed: 71 additions & 0 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/compiler/checker.ts‎

Copy file name to clipboardExpand all lines: src/compiler/checker.ts
+4Lines 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)) {
Collapse file
+25Lines 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+
Collapse file
+29Lines 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+
Collapse file
+13Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.