我想知道是否有一种方法可以解决ts错误,而无需添加和额外的宽松接口,或者将?
的id设置为可选
所以一个例子是: 假设有这个基本界面
interface Task {
id: string;
label: string;
}
然后在代码中执行此操作
const task: Task = {
label: "test"
}
Typescript Error here because it says that Task is missing ID.
↓
addTask(task);
...
const addTask = (task: Task) => {
task.id = "1";
...
}
ts输出的错误是
Argument of type '{ label: string; }' is not assignable to parameter of type 'Task'.`
有没有办法告诉打字稿这不是问题,因为之后不久就会提供ID?