OI have gotten two values from api, one is fruit and another is vegetable. I only know both type are string, but not the exactly values.
// fruit can be any string, not apple only, same on vegetable. I don't know what exactly value api return.
const fruit: string = 'apple'
const vegetable: string = 'onion'
I want to define a type Food, which can contain the substring fruit and vegetable.
As I don't know the return value from api, I cannot define a type like
type Fruit = 'apple' | 'banana'
const fruit: Fruit = 'apple
I know in this way, it's working. But my one is not Fruit, it's only string.
This is what I tried
type Fruit = typeof fruit
type Vegetable = typeof vegetable
type Food = `${Fruit} ${Vegetable} ${string}`
In this way, the type is {string} {string} {string}, the first two is not the exacly value from fruit and vegetable. The below is what I want
const test1: Food = 'a b c d'; // wrong
const test2: Food = 'apple onion c d'; // pass
How can I define that Food type if I cannot have a union type for the value, only can be a generic string type?