Hi. I have a series of related classes packaged up and exported from a single module. Something like:
// states.ts
export {default as Alabama} from "./alabama";
export {default as Alaska} from "./alaska";
//...
Now, elsewhere in the code, I want to iterate over all the related classes:
import * as States from "./states.ts"
for(let stateExportName in States) {
let thisState = States[stateExportName];
// ... do something.
}
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 a State. 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 the thisState variable as any like so:
for(let stateExportName in States) {
let thisState: any = States[stateExportName];
// ... do something.
}
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鈥攂ut also because having any compiler errors seems to prevent tslint from running with type-checking on).
possibly something like the prosal in https://github.com/Microsoft/TypeScript/issues/420 could cover this.
Another possibility is using something like keysof operator (https://github.com/Microsoft/TypeScript/pull/10425) to:
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];
Another possibility is using something like keysof operator
Ooh, that seems cool. And maybe keysof States could be automatically inferred as the type in the for-in loop? (I can see why inferring keysof arg might not work in the general case for for-in loops, because of properties up the prototype chain...but it does seem like it should work for module namespace objects, since I'm pretty sure their keys are only own keys, and those can be known statically.)
To remove the implicit any error, what you want to write is
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 Index signature is missing in type 'typeof "a"'. and I can't figure out an easy way around it, even though TypeScript _could_ infer an index type for the import. The only sort of way that would work would be to cast to any before casting to an index type, but that is type abuse, and I hate being cruel to types.
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.
Most helpful comment
To remove the implicit any error, what you want to write is