A
A
Alex Mirgorodskiy2021-03-15 11:47:10
typescript
Alex Mirgorodskiy, 2021-03-15 11:47:10

How to extract content from a custom type into a property?

Good afternoon, do not tell me how you can extract all the content from a custom type?
For example:

I have an interface with a property

export interface ContainerProps {
    xAlign?: DefaultAlignXModifiers,
}


DefaultAlignXModifiers is a type with content 'center' | 'left' | 'right'
And if xAlign does not match these properties, then I see a warning that the input data does not match the
DefaultAlignXModifiers type. And I want to see exactly what values ​​are expected ('center' | 'left' | 'right')

And I can see them if I just write
xAlign: 'center' | 'left' | 'right'

But I would like to store these variants in one variable, since they are repeated in many places, and I used type
. Is there any other way? Who has been with typeScript for a long time, tell me, otherwise I'm just mastering

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
VitalyChaikin, 2021-03-15
@AlexWeb6667

I must say right away - I am not an expert on TS, try this option:

const roArray: ReadonlyArray<string> = ["center", "left", "right"];

type DefaultAlignXModifiers =  roArray;

interface ContainerProps {
    xAlign?: DefaultAlignXModifiers,
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question