All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.l10n.javascript.rules.javascript.S6590.html Maven / Gradle / Ivy

There is a newer version: 10.17.0.28100
Show newest version

Why is this an issue?

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 automatically
  • as T where T denotes a literal type to instruct TypeScript to infer the literal type explicitly

In 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.

How to fix it

Replace the explicit literal type assertion with as const.

Code examples

Noncompliant code example

class Foo {
  public static foo: 42 = 42; // Noncompliant

  // ...
}

Compliant solution

class Foo {
  public static foo = 42 as const;

  // ...
}

Noncompliant code example

let foo = { bar: 'baz' as 'baz' };

Compliant solution

let foo = { bar: 'baz' as const };

Resources

Documentation





© 2015 - 2024 Weber Informatics LLC | Privacy Policy