Skip to content
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

Allow defining static index signature #6480

Closed
iby opened this issue Jan 14, 2016 · 12 comments · Fixed by #37797
Closed

Allow defining static index signature #6480

iby opened this issue Jan 14, 2016 · 12 comments · Fixed by #37797
Labels
Effort: Moderate Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual". Help Wanted You can do this Suggestion An idea for TypeScript
Milestone

Comments

@iby
Copy link

iby commented Jan 14, 2016

class Foo {
    [key: string]: any;
    static BAR:string = 'bar';
    static BAZ:string = 'baz';
}

console.log(Foo['BAR']);
console.log(Foo['B' + 'A' + 'Z']);
console.log(Foo['qux']);

I get the following errors with this:

tst.ts(8,13): error TS7017: Index signature of object type implicitly has an 'any' type.
tst.ts(9,13): error TS7017: Index signature of object type implicitly has an 'any' type.

I've read in the gitter archives that this should be working. Should it? How can I get a static property value using a dynamic key?

@iby
Copy link
Author

iby commented Jan 14, 2016

Figured that I can access the value of a static property using the following notation. Is that error expected though? Is that because [key: string]: any for instances only?

console.log((<any>Foo)['B' + 'A' + 'Z']);
console.log((<any>Foo)['qux']);

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript In Discussion Not yet reached consensus labels Jan 14, 2016
@RyanCavanaugh
Copy link
Member

You're correct -- the declared index signature only applies to the instance side of the class, not the constructor function itself.

Presumably we'd allow you to fix this without a type assertion by writing static [key: string]: any in the class declaration but that's currently disallowed. I don't see any obvious reason why it couldn't be allowed, though.

@mhegazy mhegazy changed the title Index signature of object type implicitly has an 'any' type with static class Allow defining static index signature Jan 14, 2016
@iby
Copy link
Author

iby commented Jan 14, 2016

Frankly static [key: string]: any was the first instinct. Tried it. Didn't work. I think it would be a very natural thing to have.

@RyanCavanaugh RyanCavanaugh added Needs More Info The issue still hasn't been fully clarified and removed In Discussion Not yet reached consensus labels Feb 1, 2016
@RyanCavanaugh
Copy link
Member

It makes sense from purely a code perspective... but what are you doing, exactly? When designing this feature we basically said "Do we ever need static index signatures?" and thought about it a bunch and came up with no user scenarios, so it'd be great to understand what kind of pattern you're writing. Thanks!

@iby
Copy link
Author

iby commented Feb 2, 2016

I have a class which acts like enum and holds a bunch of constants. Though there are string enums, this is more appropriate & convenient for my case. And I need to check if the constant is there or not, which doesn't work nicely with Foo['…'] syntax. The example above shows that in full.

@RyanCavanaugh RyanCavanaugh added In Discussion Not yet reached consensus and removed Needs More Info The issue still hasn't been fully clarified labels Feb 2, 2016
@RyanCavanaugh RyanCavanaugh added Help Wanted You can do this and removed In Discussion Not yet reached consensus labels May 9, 2016
@RyanCavanaugh RyanCavanaugh added this to the Community milestone May 9, 2016
@RyanCavanaugh RyanCavanaugh added the Effort: Moderate Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual". label May 9, 2016
@RyanCavanaugh
Copy link
Member

Accepting PRs. This should be a fairly easy change for someone familiar with the compiler, just checking for some flags during type construction.

@markspolakovs
Copy link

Has there been any progress on this in the past year? I'm running into this in a project, and it's blocking me, but I am not "familiar with the compiler", so I cannot make the fix myself.

@nikis05
Copy link

nikis05 commented Jul 9, 2017

@RyanCavanaugh Obviously need this feature. My usecase:
• I write a framework that offers a class that is to be extended by the user;
• The extended class can have additional methods, both instance (no problem with this) and static;
• The framework needs to operate on these methods added by the user, aka

let method = isMethodStatic ? ExtendedClass[methodName] : new ExtendedClass(someData)[methodName]

Writing ExtendedClass[methodName] is currently impossible and there is no elegant workaround.

@kitsonk
Copy link
Contributor

kitsonk commented Jul 10, 2017

Accepting PRs means that the core team do not have the time to address this and it is open to the community to solve.

@Kingwl
Copy link
Contributor

Kingwl commented Apr 9, 2020

friendly ping @ianbytchek @nikis05 @markspolakovs

I have a pr for this one.
You could take a preview here(if you still need that).

@hengkx
Copy link

hengkx commented Jun 30, 2020

same problem.

@DanielRosenwasser
Copy link
Member

Thanks @Kingwl!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Effort: Moderate Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual". Help Wanted You can do this Suggestion An idea for TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants