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

Allow const assertions in no-object-literal-type-assertion #4681

Merged
merged 4 commits into from May 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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> ({});
asmundg marked this conversation as resolved.
Show resolved Hide resolved
#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