Skip to content

Bugs with enum (import and typing) #3507

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
loucyx opened this issue Jun 14, 2015 · 3 comments
Closed

Bugs with enum (import and typing) #3507

loucyx opened this issue Jun 14, 2015 · 3 comments
Labels
By Design Deprecated - use "Working as Intended" or "Design Limitation" instead

Comments

@loucyx
Copy link

loucyx commented Jun 14, 2015

When I import a module that has an enum and then I pass a string variable as index, with the noImplicitAny flag set to true, I get an error of implicit any in the index signature.

Examples:

This works but it has an unwanted behavior detailed in the comment:

enum Foobar {
    "foo",
    "bar"
}

var consoleFoobar = (index: string) => {
    // output is of type any. It should be number if the index is string and string if the index is number
    var output = Foobar[index];
    console.log(output);
}

This trows an error:

// a-module.ts
export enum Foobar {
    "foo",
    "bar"
}

// app.ts
import { Foobar } from "./a-module";

var consoleFoobar = (index: string) => {
    var output = Foobar[index]; // Error: Index signature of object type implicitly has an 'any' type.
    console.log(output);
}
@DanielRosenwasser
Copy link
Member

Sounds like a dupe of #1232. You may want to look into --suppressImplicitAnyIndexErrors.

--suppressImplicitAnyIndexErrors   Suppress noImplicitAny errors for indexing objects lacking index signatures.

See:

@loucyx
Copy link
Author

loucyx commented Jun 15, 2015

I don't want to suppress the implicit any errors (they are useful), the thing is that enum is trowing errors and it shuldn't.

Let's say I have an enum like this: enum EnumExample { "value" }. When I write EnumExample[0] I should get a string ("value" in this case) or undefined, and when I write EnumExample["value"] I should get a number (0 in this case) or undefined. The bug is that I get an any if the enum is in the same file, and if I load the enum from a module, It trows an index error (error that doesn't trow if is in the same file).

@danquirk
Copy link
Member

danquirk commented Jul 8, 2015

@lucasmciruzzi with the latest bits in master I see the same behavior in both scenarios (the single file and multi file case). The type of output is any and no errors are reported.

See #592 for why we give an any here.

@danquirk danquirk closed this as completed Jul 8, 2015
@danquirk danquirk added the By Design Deprecated - use "Working as Intended" or "Design Limitation" instead label Jul 8, 2015
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
By Design Deprecated - use "Working as Intended" or "Design Limitation" instead
Projects
None yet
Development

No branches or pull requests

3 participants