Tslint: Linting for enum types with string values

Created on 5 Jul 2017  路  8Comments  路  Source: palantir/tslint

Bug Report

  • __TSLint version__:
  • __TypeScript version__:
  • __Running TSLint via__: (pick one) CLI / Node.js API / VSCode / grunt-tslint / Atom / Visual Studio / etc

TypeScript code being linted

Currently from TypeScript 2.4 we have the options to use enums like:

enum Colors {
    Red = "RED",
    Green = "GREEN",
    Blue = "BLUE",
}

This however gives still gives the warning even with the newest version ot the linter:
[ts] Type '"RED"' is not assignable to type 'Colors'
with tslint.json configuration:

Standart tslint file generated from angular-cli.

Is there an option to fix this or is it an option that is coming

Actual behavior

Expected behavior

Do not show this warning.

External

All 8 comments

I don't really understand your problem description. Where is the error located, it this the whole repro code, what version of typescript are you using?

From what I understand I would say that this an error coming from typescript rather than tslint.
I'd guess you have the wrong version of typescript installed.
When I last used the angular-cli to generate a project, it didn't support [email protected] and used [email protected] instead to compile my app.

+1 to this issue.

Following angular/angular-cli#6827 . It says that you should upgrade Rxjs to 5.4.2. However, at this time on package.json, the version used is ^5.1.0, so 6.0.0-alpha.0. Theoretically, you just have to set typescript to ~4.1.0 . It compiles, but tslint displays the enum string syntax as a warning. Futhermore, Typescript 4.2 is unstable with angular-cli, so except for an experimental project about it, you should not use it.

Waiting this issue fixed (and waiting the support on angular-cli), you can use this sexy trick to implement your own string enum : http://angularfirst.com/typescript-string-enums/

@MLobry looking at the linked angular-cli issue, it seems like [email protected] is now supported.

Please update typescript to 2.4.1 to use string enums.

If that doesn't help, I'd need an example repository to reproduce the error.

closing as this is not a tslint-related issue

Of course this is a tslint issue. The error says as much. I'm on TypeScript version 2.5.3, TsLint 5.7.0, and cannot assign string values to enums exactly as described above.

@banderberg We need the exact error message, the rule name and the code that's causing it. Otherwise we can only guess what's wrong.

Hi @ajafff , here is exact error I can see in hint:

TS2322:Type '"REQUESTER"' is not assignable to type 'Roles'.

For next code:

export enum Roles {
    Requester = 'REQUESTER'
}

Typescript 2.6.1
Tslint 5.8.0

That is a typescript compiler error. You can tell because the error string begins with "TS".

Was this page helpful?
0 / 5 - 0 ratings