Typescript - Why can't this string literal type be inferred?
问题 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 []. Try it It