версия машинописного текста: 3.7.5
type A = { _id: string }
type B = A & { one: string, two: string }
type C = Omit<B, "_id">;
const valueWithoutId: C = {one: "value-1", two: "value-2"};
// This works just fine...
const valueWithId: B = {...valueWithoutId, _id: "new-id"};
console.log(valueWithId);
function thisBreaks_butWhy<T extends A, U = Omit<T, "_id">>(_valueWithoutId: U) {
// The following line breaks... Why?
const _valueWithId: T = {..._valueWithoutId, _id: "new-id"};
}
Я не понимаю, почему, когда я ограничиваю типы, я все еще получаю следующую ошибку:
TS2322: Type 'U & { _id: string; }' is not assignable to type 'T'.
'U & { _id: string; }' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'A'
Я пытался быть четным более точно:
function thisBreaks_butWhy<T extends A>(_valueWithoutId: Omit<T, "_id">) {
// The following line breaks... Why?
const _valueWithId: T = {..._valueWithoutId, _id: "new-id"};
}
в отношении типа, здесь не может быть ошибки (я думаю) ... так почему это происходит?