Tslint: no-object-literal-type-assertion should have an option to allow const assertions

Created on 2 Apr 2019  路  2Comments  路  Source: palantir/tslint

Option to rule Suggestion

Since TypeScript 3.4 there are "const contexts for literal expressions" (Microsoft/TypeScript#29510) that allow an as const statement in certain scenarios.

Right now the rule report an error:

// Type assertion on object literals is forbidden, use a type annotation instead.
let bar = { type: 'foo' } as const;

The new const assertions are really useful for complex type constructs, specially if they need to be used in discriminated unions.

So, add an option like "allow-const-assertions" to support this case would be very nice.

Accepting PRs Rule Enhancement

Most helpful comment

Actually these assertions feel a little different to me than type assertions, so I'm tempted to just allow as const by default in the rule. Maybe if someone wants to ban them we can add that option later.

All 2 comments

Actually these assertions feel a little different to me than type assertions, so I'm tempted to just allow as const by default in the rule. Maybe if someone wants to ban them we can add that option later.

@adidahiya Even better :wink:

Was this page helpful?
0 / 5 - 0 ratings