eslint / typescript-eslint-parser

An ESLint custom parser which leverages TypeScript ESTree to allow for ESLint to lint TypeScript source code.
Other
915 stars 92 forks source link

Using number as enum variable initializer should be treated as a number rather than literal. #470

Closed vivekgalatage closed 6 years ago

vivekgalatage commented 6 years ago

What version of TypeScript are you using? 2.8.1

What version of typescript-eslint-parser are you using? 15.0.0

What code were you trying to parse?

enum Test { 
  one = 1, 
  two = 1.1, 
  three = 1.2 
}

What did you expect to happen? The enum initializer type is number whereas the generated JSON states it to be a string literal. The typescript compiler treats this as a number only.

What happened? image

j-f1 commented 6 years ago

That’s the correct AST — see this example with just a number.

vivekgalatage commented 6 years ago

This also applies to the decorators where the argument 3.14 is treated as Literal

function $scope(scope: any[], chart: string, pi: number) {
  return (target: Function) => {}
}

@$scope(['hello'], 'bar', 3.14)
export class ExportedClass {
}
vivekgalatage commented 6 years ago

This gist [1] specifies the type as NumericLiteral

[1] https://astexplorer.net/#/gist/6e9911fc9df41ff778b9c4b90eeafa42/a60adb3256a228b42c31ec9add791676f2512dfe

j-f1 commented 6 years ago

@vivekgalatage The TypeScript parser doesn’t follow the ESTree spec, which specified that all literales are of the Literal type.

vivekgalatage commented 6 years ago

@j-f1 sure, thanks for the clarification. So in that case, how does the developer would differentiate between the numerical values vs the literal values. Or are we not supposed to use the non literal values at these places?

j-f1 commented 6 years ago

String literals have a string value, and numeric literals have a numeric value.

vivekgalatage commented 6 years ago

Fantastic!!! And my apologies for missing this info. Thank you!