简体   繁体   中英

String Literal Type Conversion In JSX and Typescript

I'm having a problem converting a string to a string literal.

The string literal is defined as follows:

type myLiteral = "cats" | "dogs"

Then I have a component that I want to pass a type from an 'inputted' string.

const myString:string = "cats"
const notQuiteSure:any = myString as myLiteral

React.createComponent(<myComponent inputtedType={notQuiteSure}/>

I'm getting squigglies all over the place. I have tried using the as keyword and the is keyword to do a 'cast' but nothing seems to work.

Anybody have any ideas how to make this work?

这样尝试

const myString: myLiteral = "cats";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM