Skip to content
This repository was archived by the owner on Mar 25, 2021. It is now read-only.

Commit d79cd18

Browse files
asmundgJosh Goldberg
authored and
Josh Goldberg
committed
Allow const assertions in no-object-literal-type-assertion (#4681)
* Ignore const assertions These are not normal type assertions and asserting that literal objects are immutable is a normal usage pattern. * Bump typescript to get utility function for const assert * Make sure tests pass on older typescripts * Clean up tests and doc
1 parent 5a3a640 commit d79cd18

File tree

4 files changed

+16
-6
lines changed

4 files changed

+16
-6
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
"tslint-config-prettier": "^1.18.0",
7474
"tslint-plugin-prettier": "^2.0.1",
7575
"tslint-test-config-non-relative": "file:test/external/tslint-test-config-non-relative",
76-
"typescript": "~3.1.6",
76+
"typescript": "~3.4.0",
7777
"yarn-deduplicate": "^1.1.1"
7878
},
7979
"engines": {

src/rules/noObjectLiteralTypeAssertionRule.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ export class Rule extends Lint.Rules.AbstractRule {
4848
The type assertion in the latter case is either unnecessary or hides an error.
4949
The compiler will warn for excess properties with this syntax, but not missing required fields.
5050
For example: \`const x: { foo: number } = {}\` will fail to compile, but
51-
\`const x = {} as { foo: number }\` will succeed.`,
51+
\`const x = {} as { foo: number }\` will succeed.
52+
Additionally, the const assertion \`const x = { foo: 1 } as const\`,
53+
introduced in TypeScript 3.4, is considered beneficial and is ignored by this rule.`,
5254
optionsDescription: Lint.Utils.dedent`
5355
One option may be configured:
5456
@@ -86,6 +88,8 @@ function walk(ctx: Lint.WalkContext<Options>): void {
8688
if (
8789
isAssertionExpression(node) &&
8890
node.type.kind !== ts.SyntaxKind.AnyKeyword &&
91+
// Allow const assertions, introduced in TS 3.4
92+
!(ts.isConstTypeReference !== undefined && ts.isConstTypeReference(node.type)) &&
8993
// Compare with UnknownKeyword if using TS 3.0 or above
9094
(!!(ts.SyntaxKind as any).UnknownKeyword
9195
? node.type.kind !== (ts.SyntaxKind as any).UnknownKeyword

test/rules/no-object-literal-type-assertion/default/test.ts.lint

+6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ x as T;
1616
{} as unknown;
1717
<unknown> {};
1818

19+
#if typescript >= 3.4.0
20+
// Allow const assertion
21+
({}) as const;
22+
<const> ({});
23+
#endif
24+
1925
foo({} as T);
2026
~~~~~~~ [0]
2127

yarn.lock

+4-4
Original file line numberDiff line numberDiff line change
@@ -1851,10 +1851,10 @@ type-detect@^1.0.0:
18511851
resolved "https://registry.yarnpkg.com/type-detect/-/type-detect-1.0.0.tgz#762217cc06db258ec48908a1298e8b95121e8ea2"
18521852
integrity sha1-diIXzAbbJY7EiQihKY6LlRIejqI=
18531853

1854-
typescript@~3.1.6:
1855-
version "3.1.6"
1856-
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.1.6.tgz#b6543a83cfc8c2befb3f4c8fba6896f5b0c9be68"
1857-
integrity sha512-tDMYfVtvpb96msS1lDX9MEdHrW4yOuZ4Kdc4Him9oU796XldPYF/t2+uKoX0BBa0hXXwDlqYQbXY5Rzjzc5hBA==
1854+
typescript@~3.4.0:
1855+
version "3.4.5"
1856+
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.4.5.tgz#2d2618d10bb566572b8d7aad5180d84257d70a99"
1857+
integrity sha512-YycBxUb49UUhdNMU5aJ7z5Ej2XGmaIBL0x34vZ82fn3hGvD+bgrMrVDpatgz2f7YxUMJxMkbWxJZeAvDxVe7Vw==
18581858

18591859
uglify-js@^3.1.4:
18601860
version "3.5.4"

0 commit comments

Comments
 (0)