我有一个关于打字类型推断的问题.这里有一个简单的例子.
interface Student {
readonly ids: number[],
}
const ids: readonly number[] = [1, 2, 3];
let student: Student = { ids: ids };
此代码片段显示以下错误消息:
The type 'readonly number[]' is 'readonly' and cannot be assigned to the mutable type 'number[]'.
似乎类型推断要求{ ids: ids }
内的值为ids
的类型必须具有number[]
而不是readonly number[]
的类型.
我可以把const ids: readonly number[] = [1, 2, 3];
中的readonly
go 掉,或者把let student: Student = { ids: ids };
改成let student: Student = { ids: ids.map(id => id) };
.