I have this type
type Abc = 'a' | 'b' | 'c'
how can I make myObj.something
type Abc
?
I can think of valueOf:
const myObj = {
something: ['a', 'b', 'c'] as valueOf Abc
}
but I wonder why it doesn't work
CodePudding user response:
in your case you are trying to set ['a', 'b', 'c']
(which is an array) as Abc
. You should define myObj.something as an array of type Abc
, with Abc[]
const myObj = {
something: ['a', 'b', 'c'] as Abc[]
}
CodePudding user response:
Please make a interface first. Then use it. Like this
type Abc = 'a' | 'b' | 'c'
// make interface
interface myObject {
something: Abc[]
}
// use the interface
const myObj:myObject = {
something: ['a']
}