The following snippet does not pass the type check:
type TaskType = 'SIMPLE' | 'COMPLEX'
interface TaskDefinition {
name: string,
task: string,
taskType: TaskType
};
const test: TaskDefinition = {
name: '',
task: '',
taskType: 'SIMPLE' // This is fine
};
const tasks : TaskDefinition[] = ["apples", "pears"].map(i => {
return {
name: i,
task: i,
taskType: 'SIMPLE' // This one is not
};
})
{ name: string; task: string; taskType: string; }[] is not assignable to type TaskDefinition[].
It seems that taskType
gets inferred as string
instead of TaskType
despite the target type being TaskDefinition
What's causing this and how can I fix it?
.map<TaskDefinition>
is probably the most elegant solution for this. – Intussuscept