-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Typing the Index Signature of a Module Namespace Object #10998
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
Comments
possibly something like the prosal in #420 could cover this. Another possibility is using something like let stateExportName : keysof States;
for(stateExportName in States) {
let thisState = States[stateExportName]; // typeof States.Alabama | States.Alaska
// ... do something.
} |
To remove the implicit any error, what you want to write is let thisState = (<any>States)[stateExportName]; |
Ooh, that seems cool. And maybe
Thanks @RyanCavanaugh! That's the one permutation I didn't try :P |
Ran across a similiar problem where some sort of inferrance of the index signature would be great. Trying to "rollup" some modules into a larger map is currently not possible. Here is an example of the problem we are facing. Given a.ts: export const foo = 'foo';
export const bar = 'bar';
export const baz = 'baz'; And then in b.ts: import * as a from './a';
const qat: { [key: string]: { [key: string]: string } } = {
a
}; Produces |
For sanity purposes, as it seems there are at least 3 open issues on this, @RyanCavanaugh indicates that an implicit index type can be inferred. |
This is possible today if the indexing type is |
Hi. I have a series of related classes packaged up and exported from a single module. Something like:
Now, elsewhere in the code, I want to iterate over all the related classes:
Hold aside that iteration over a commonjs module's export names would use a for-in, whereas iterating over an es6 module namespace object's exports would use a for-of (since es6 does define @@iterator on module namespace objects)... in either case, this iteration should be possible.
I'd like to be able to provide a type for the index signature of the module namespace object, so that
thisState
is automatically inferred as aState
. I asked on StackOverflow, and @basarat said that wasn't possible, so I'm opening an issue here for it as a potential feature request. It does seem like an edge case, so I'm not sure how worth solving it is, but it is something I bumped into.The other thing is that, if I use the code as written above, I get an
"Index signature of object type implicitly has an 'any' type"
error. That makes sense, since I haven't defined an index signature for the module anywhere. However, I get that error even if I explicitly type thethisState
variable asany
like so:I can't tell if that's a bug or expected behavior. If it is expected behavior, though, then I have to do some trickery to silence the compiler error...which is really annoying (aesthetically—but also because having any compiler errors seems to prevent tslint from running with type-checking on).
The text was updated successfully, but these errors were encountered: