Skip to content

Octal literals for enum members treated as decimal #11298

Description

@lucaswerkmeister

TypeScript Version: 2.0.3

Code

enum X { A = 010 }
console.log(X.A === 010);

Expected behavior:

Prints true010 is an octal literal, both inside and outside an enum declaration.

Actual behavior:

Prints false: In the enum declaration, 010 is understood as 10. This is because evalConstant in src/compiler/checker.ts parses the expression with

case SyntaxKind.NumericLiteral:
    return +(<NumericLiteral>e).text;

and while +010 is 8, +"010" is 10.

It’s possible that this behavior is already being used by some to zero-pad enum literal values (though padding is probably more useful for hexadecimal literals), and so it might be a good idea to treat this as an undocumented feature instead of a bug, and to “fix” it by documenting this behavior in the language specification (under 9.2 Enum Members, in the _constant enum members_ definition).

This is similar to #10101, but I think it’s a different bug.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Breaking ChangeWould introduce errors in existing codeBugA bug in TypeScriptFixedA PR has been merged for this issueHelp WantedYou can do this

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions