Repository navigation
Typing the Index Signature of a Module Namespace Object #10998
Description
Activity
possibly something like the prosal in #420 could cover this.
Another possibility is using something like
keysofoperator (#10425) to:let stateExportName : keysof States; for(stateExportName in States) { let thisState = States[stateExportName]; // typeof States.Alabama | States.Alaska // ... do something. }
RyanCavanaugh commented
on Sep 19, 2016 MemberMore actionsTo remove the implicit any error, what you want to write is
let thisState = (<any>States)[stateExportName];
Reacted by dan-def, Kirill Uksusov, Jamie Birch, Alex Sharp, R Thomazella, Rinat Rezyapov, Mohit Tilwani, Kaan Çalışkan and Bryan HoangReacted by Chilli!ethanresnick commented
on Sep 20, 2016 ContributorAuthorMore actionsAnother possibility is using something like keysof operator
Ooh, that seems cool. And maybe
keysof Statescould be automatically inferred as the type in the for-in loop? (I can see why inferringkeysof argmight 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 Ryan Cavanaugh (@RyanCavanaugh)! That's the one permutation I didn't try :P
- addedSuggestionAn idea for TypeScriptAn idea for TypeScriptNeeds ProposalThis issue needs a plan that clarifies the finer details of how it could be implemented.This issue needs a plan that clarifies the finer details of how it could be implemented.
on Sep 23, 2016 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 toanybefore casting to an index type, but that is type abuse, and I hate being cruel to types.Reacted by Dusty Greif, Mikhail, KaMeHb-UA and Kentleigh EnglishFor sanity purposes, as it seems there are at least 3 open issues on this, Ryan Cavanaugh (@RyanCavanaugh) indicates that an implicit index type can be inferred.
RyanCavanaugh commented
on Jun 23, 2021 MemberMore actionsThis is possible today if the indexing type is
keyofthe containing object. Inferring an index signature would be problematic due to it not being legal to write to additional new keys, so I think the current behavior is what we want.
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 (@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
thisStateis automatically inferred as aState. I asked on StackOverflow, and Basarat Ali Syed (@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 thethisStatevariable asanylike 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).