Si tengo una interfaz en mecanografiado así
interface MyInterface { id: number; image: string; text: "one thing" | "another" | "anotherthing" | "anotherthing" }¿Cómo puedo factorizar mi tipo de texto con las canalizaciones para poder usarlo varias veces como tipo en mi programa? Parece que debería ser simple, pero no estoy seguro de cuál es la sintaxis.
Para mayor claridad, me gustaría poder usarlo como
factoredOutType = "one thing" | "another" | "anotherthing" | "anotherthing" interface MyInterface1 { id: number; image: string; text: factoredOutType } interface MyInterface2 { name: factoredOutType }etc... Parece que no puedo encontrar la sintaxis de esta pregunta, por lo que se agradece la ayuda.
Creo que solo necesitas la palabra clave type :
type factoredOutType = "one thing" | "another" | "anotherthing" | "anotherthing"No está cerca de un compilador ahora, así que realmente no puedo verificarlo, pero creo que funciona
puede formatear tipos de literales de cadena utilizando el literal de plantilla si sus tipos están relacionados o son dinámicos. p.ej
type stringTypes = `${'hello' | 'world'}_id`; const a: stringTypes = 'hello_id';