We are currently developing a Vue 3 component library, and as the project grows in scale, we have refactored it with TypeScript to improve the development experience. However, we are facing a challenge. Our library is being used by teams in different environments, some with TypeScript and bundler setups, and others without. This means that for almost every component, we need to validate prop types both at compile-time and runtime.
props: {
someProp: {
type: String as PropType<'enum1' | 'enum2' | 'enum3'>,
validator: (v: string) => {
return ['enum1', 'enum2', 'enum3'].includes(v)
}
}
}
While the code is functional and beneficial for both TypeScript and JavaScript teams, as it provides code completion and error detection capabilities, we are not satisfied with the duplication of code. Despite <'enum1' | ...>
being a type definition and ['enum1', ...]
being an array, they essentially contain the same information but are written repeatedly.
Is there a way to streamline the process and define props with PropType
and validator
without unnecessary repetition?