From d79cd18d9e83234e00ee35fc4dc18fc791a3d4bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=85smund=20Grammeltvedt?= Date: Tue, 7 May 2019 14:31:20 +0200 Subject: [PATCH] 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 --- package.json | 2 +- src/rules/noObjectLiteralTypeAssertionRule.ts | 6 +++++- .../no-object-literal-type-assertion/default/test.ts.lint | 6 ++++++ yarn.lock | 8 ++++---- 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index 4f387121a59..a37ddf25ab2 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/rules/noObjectLiteralTypeAssertionRule.ts b/src/rules/noObjectLiteralTypeAssertionRule.ts index 2db26eb09da..46e0145059b 100644 --- a/src/rules/noObjectLiteralTypeAssertionRule.ts +++ b/src/rules/noObjectLiteralTypeAssertionRule.ts @@ -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: @@ -86,6 +88,8 @@ function walk(ctx: Lint.WalkContext): 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 diff --git a/test/rules/no-object-literal-type-assertion/default/test.ts.lint b/test/rules/no-object-literal-type-assertion/default/test.ts.lint index eaebb211673..53a555a4710 100644 --- a/test/rules/no-object-literal-type-assertion/default/test.ts.lint +++ b/test/rules/no-object-literal-type-assertion/default/test.ts.lint @@ -16,6 +16,12 @@ x as T; {} as unknown; {}; +#if typescript >= 3.4.0 +// Allow const assertion +({}) as const; + ({}); +#endif + foo({} as T); ~~~~~~~ [0] diff --git a/yarn.lock b/yarn.lock index d97ba9dd553..9f9165d00d9 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"