Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d07eb9e

Browse files
authored
fix(eslint-plugin): [no-unsafe-assignment] handle shorthand property assignment (typescript-eslint#8800)
* fix(eslint-plugin): [no-unsafe-assignment] handle shorthand property assignment * fix formatting
1 parent 21708bb commit d07eb9e

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

packages/eslint-plugin/tests/rules/no-unsafe-assignment.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,11 @@ declare function Foo(props: { a: string }): never;
157157
'const x: Set<unknown> = y as Set<any>;',
158158
// https://github.com/typescript-eslint/typescript-eslint/issues/2109
159159
'const x: Map<string, string> = new Map();',
160+
`
161+
type Foo = { bar: unknown };
162+
const bar: any = 1;
163+
const foo: Foo = { bar };
164+
`,
160165
],
161166
invalid: [
162167
{
@@ -389,5 +394,20 @@ const test: T = ['string', []] as any;
389394
},
390395
],
391396
},
397+
{
398+
code: `
399+
type Foo = { bar: number };
400+
const bar: any = 1;
401+
const foo: Foo = { bar };
402+
`,
403+
errors: [
404+
{
405+
messageId: 'anyAssignment',
406+
line: 4,
407+
column: 20,
408+
endColumn: 23,
409+
},
410+
],
411+
},
392412
],
393413
});

packages/type-utils/src/getContextualType.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ export function getContextualType(
2424
return parent.type ? checker.getTypeFromTypeNode(parent.type) : undefined;
2525
} else if (ts.isJsxExpression(parent)) {
2626
return checker.getContextualType(parent);
27-
} else if (ts.isPropertyAssignment(parent) && ts.isIdentifier(node)) {
27+
} else if (
28+
ts.isIdentifier(node) &&
29+
(ts.isPropertyAssignment(parent) ||
30+
ts.isShorthandPropertyAssignment(parent))
31+
) {
2832
return checker.getContextualType(node);
2933
} else if (
3034
ts.isBinaryExpression(parent) &&

0 commit comments

Comments
 (0)