TypeScript provides two ways to tell the compiler that a literal value should be typed as a literal type like 42 rather than the primitive one number:
as const tells TypeScript to infer the literal type automaticallyas T where T denotes a literal type to instruct TypeScript to infer the literal type explicitlyIn practice, as const is preferred because the type checker doesn’t need re-typing the literal value.
Therefore, the rule flags occurrences of explicit literal types that can be replaced with an as const assertion.
Replace the explicit literal type assertion with as const.
class Foo {
public static foo: 42 = 42; // Noncompliant
// ...
}
class Foo {
public static foo = 42 as const;
// ...
}
let foo = { bar: 'baz' as 'baz' };
let foo = { bar: 'baz' as const };