Using a type as indexer
up vote
0
down vote
favorite
I have set an enum like this:
type Mode = 'Auto' | 'Manual';
I now want to create an object where they keys must be one of the strings from Mode enum.
I tried this:
class Form extends React.Component {
fields:
[index: ExecutionMode]: string
=
Auto: ,
Manual: ,
;
However this gives me error:
[ts] An index signature parameter type cannot be a type alias. Consider writing '[index: string]: string' instead.
Is this possible in typescript?
typescript
add a comment |
up vote
0
down vote
favorite
I have set an enum like this:
type Mode = 'Auto' | 'Manual';
I now want to create an object where they keys must be one of the strings from Mode enum.
I tried this:
class Form extends React.Component {
fields:
[index: ExecutionMode]: string
=
Auto: ,
Manual: ,
;
However this gives me error:
[ts] An index signature parameter type cannot be a type alias. Consider writing '[index: string]: string' instead.
Is this possible in typescript?
typescript
2
Possible duplicate of TypeScript: typings based on object keys that come from enum
– jcalz
Nov 10 at 1:19
2
Why you call this enumtype Mode = 'Auto' | 'Manual';when it is a union? Did you mis-call it, or did you wrote wrong code here.
– Nurbol Alpysbayev
Nov 10 at 1:30
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have set an enum like this:
type Mode = 'Auto' | 'Manual';
I now want to create an object where they keys must be one of the strings from Mode enum.
I tried this:
class Form extends React.Component {
fields:
[index: ExecutionMode]: string
=
Auto: ,
Manual: ,
;
However this gives me error:
[ts] An index signature parameter type cannot be a type alias. Consider writing '[index: string]: string' instead.
Is this possible in typescript?
typescript
I have set an enum like this:
type Mode = 'Auto' | 'Manual';
I now want to create an object where they keys must be one of the strings from Mode enum.
I tried this:
class Form extends React.Component {
fields:
[index: ExecutionMode]: string
=
Auto: ,
Manual: ,
;
However this gives me error:
[ts] An index signature parameter type cannot be a type alias. Consider writing '[index: string]: string' instead.
Is this possible in typescript?
typescript
typescript
edited Nov 10 at 1:20
asked Nov 10 at 1:15
Noitidart
17.3k1358148
17.3k1358148
2
Possible duplicate of TypeScript: typings based on object keys that come from enum
– jcalz
Nov 10 at 1:19
2
Why you call this enumtype Mode = 'Auto' | 'Manual';when it is a union? Did you mis-call it, or did you wrote wrong code here.
– Nurbol Alpysbayev
Nov 10 at 1:30
add a comment |
2
Possible duplicate of TypeScript: typings based on object keys that come from enum
– jcalz
Nov 10 at 1:19
2
Why you call this enumtype Mode = 'Auto' | 'Manual';when it is a union? Did you mis-call it, or did you wrote wrong code here.
– Nurbol Alpysbayev
Nov 10 at 1:30
2
2
Possible duplicate of TypeScript: typings based on object keys that come from enum
– jcalz
Nov 10 at 1:19
Possible duplicate of TypeScript: typings based on object keys that come from enum
– jcalz
Nov 10 at 1:19
2
2
Why you call this enum
type Mode = 'Auto' | 'Manual'; when it is a union? Did you mis-call it, or did you wrote wrong code here.– Nurbol Alpysbayev
Nov 10 at 1:30
Why you call this enum
type Mode = 'Auto' | 'Manual'; when it is a union? Did you mis-call it, or did you wrote wrong code here.– Nurbol Alpysbayev
Nov 10 at 1:30
add a comment |
1 Answer
1
active
oldest
votes
up vote
3
down vote
accepted
Here you are
type Mode = 'Auto' | 'Manual';
type Fields =
[K in Mode]: string
// inside your class:
fields: Fields =
Auto: ,
Manual:
BTW, what you are calling "enum" is a "union".
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
Here you are
type Mode = 'Auto' | 'Manual';
type Fields =
[K in Mode]: string
// inside your class:
fields: Fields =
Auto: ,
Manual:
BTW, what you are calling "enum" is a "union".
add a comment |
up vote
3
down vote
accepted
Here you are
type Mode = 'Auto' | 'Manual';
type Fields =
[K in Mode]: string
// inside your class:
fields: Fields =
Auto: ,
Manual:
BTW, what you are calling "enum" is a "union".
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
Here you are
type Mode = 'Auto' | 'Manual';
type Fields =
[K in Mode]: string
// inside your class:
fields: Fields =
Auto: ,
Manual:
BTW, what you are calling "enum" is a "union".
Here you are
type Mode = 'Auto' | 'Manual';
type Fields =
[K in Mode]: string
// inside your class:
fields: Fields =
Auto: ,
Manual:
BTW, what you are calling "enum" is a "union".
answered Nov 10 at 1:32
Nurbol Alpysbayev
2,1801220
2,1801220
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53235195%2fusing-a-type-as-indexer%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Possible duplicate of TypeScript: typings based on object keys that come from enum
– jcalz
Nov 10 at 1:19
2
Why you call this enum
type Mode = 'Auto' | 'Manual';when it is a union? Did you mis-call it, or did you wrote wrong code here.– Nurbol Alpysbayev
Nov 10 at 1:30