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

Commit

Permalink
Allow const assertions in no-object-literal-type-assertion (#4681)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
asmundg authored and Josh Goldberg committed May 7, 2019
1 parent 5a3a640 commit d79cd18
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -73,7 +73,7 @@
"tslint-config-prettier": "^1.18.0",
"tslint-plugin-prettier": "^2.0.1",
"tslint-test-config-non-relative": "file:test/external/tslint-test-config-non-relative",
"typescript": "~3.1.6",
"typescript": "~3.4.0",
"yarn-deduplicate": "^1.1.1"
},
"engines": {
Expand Down
6 changes: 5 additions & 1 deletion src/rules/noObjectLiteralTypeAssertionRule.ts
Expand Up @@ -48,7 +48,9 @@ export class Rule extends Lint.Rules.AbstractRule {
The type assertion in the latter case is either unnecessary or hides an error.
The compiler will warn for excess properties with this syntax, but not missing required fields.
For example: \`const x: { foo: number } = {}\` will fail to compile, but
\`const x = {} as { foo: number }\` will succeed.`,
\`const x = {} as { foo: number }\` will succeed.
Additionally, the const assertion \`const x = { foo: 1 } as const\`,
introduced in TypeScript 3.4, is considered beneficial and is ignored by this rule.`,
optionsDescription: Lint.Utils.dedent`
One option may be configured:
Expand Down Expand Up @@ -86,6 +88,8 @@ function walk(ctx: Lint.WalkContext<Options>): void {
if (
isAssertionExpression(node) &&
node.type.kind !== ts.SyntaxKind.AnyKeyword &&
// Allow const assertions, introduced in TS 3.4
!(ts.isConstTypeReference !== undefined && ts.isConstTypeReference(node.type)) &&
// Compare with UnknownKeyword if using TS 3.0 or above
(!!(ts.SyntaxKind as any).UnknownKeyword
? node.type.kind !== (ts.SyntaxKind as any).UnknownKeyword
Expand Down
Expand Up @@ -16,6 +16,12 @@ x as T;
{} as unknown;
<unknown> {};

#if typescript >= 3.4.0
// Allow const assertion
({}) as const;
<const> ({});
#endif

foo({} as T);
~~~~~~~ [0]

Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Expand Up @@ -1851,10 +1851,10 @@ type-detect@^1.0.0:
resolved "https://registry.yarnpkg.com/type-detect/-/type-detect-1.0.0.tgz#762217cc06db258ec48908a1298e8b95121e8ea2"
integrity sha1-diIXzAbbJY7EiQihKY6LlRIejqI=

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

uglify-js@^3.1.4:
version "3.5.4"
Expand Down

0 comments on commit d79cd18

Please sign in to comment.