gerichhome
gerichhome

Reputation: 2192

No compile time TypeScript errors for enums

This looks strange for me but TypeScript 0.9.5 compiler does not generate any compile time errors when I write something like:

enum A {
    a,
    b,
    c,
}

var x : A = 20;

To compare C# compiler will raise the following error: Cannot implicitly convert type 'int' to 'System.Security.AccessControl.AccessControlSections'. An explicit conversion exists (are you missing a cast?)

As for me it would be better to specify such cast explicitly:

var x : A = <A>20;

Is it an intentional design and is required in some use case or this was jut missed?

Upvotes: 2

Views: 154

Answers (2)

Ryan Cavanaugh
Ryan Cavanaugh

Reputation: 221004

It's intentional. The fact that the C# compiler knows what you meant ("are you missing a cast?") is instructive -- in general, TypeScript errs on the side of flexibility over strictness. Some people want a cast to be required here "to show that you thought about it"; the assumption in this case is that you are always thinking while programming and should not be bothered unless something is more obviously wrong.

Upvotes: 2

basarat
basarat

Reputation: 275927

This is by design. Numbers can be assigned to enums without a cast and vice versa.

Upvotes: 0

Related Questions