see also: TS playground
<code>const objectKyes = <T extends {},>(obj: T) => Object.keys(obj) as Array<keyof T>;
const bigObject = {
a: 1,
b: '2',
c: [3],
};
const smallOne = {
a: 1,
b: '2',
} satisfies Partial<typeof bigObject>;
// same as Object.assign(bigObject, smallOne)
// Type 'string | number' is not assignable to type 'never'.
// I can understand, `bigObject[key]` => `bigObject['a' | 'b']` => `number | string`, if `smallOne[key]` wants to assign to `bigObject[key]`, it must be `string & numer` => never
objectKyes(smallOne).forEach(key => bigObject[key] = smallOne[key]);
// Property 'c' is missing in type '{ a: number; b: string; }' but required in type '{ a: number; b: string; c: number[]; }'.
// Here I can't understand, the type T must be either 'a' or 'b', so does it need attribute 'c'
objectKyes(smallOne).forEach(<T extends keyof (typeof smallOne),>(key: T) => bigObject[key] = smallOne[key]);
</code>
<code>const objectKyes = <T extends {},>(obj: T) => Object.keys(obj) as Array<keyof T>;
const bigObject = {
a: 1,
b: '2',
c: [3],
};
const smallOne = {
a: 1,
b: '2',
} satisfies Partial<typeof bigObject>;
// same as Object.assign(bigObject, smallOne)
// Type 'string | number' is not assignable to type 'never'.
// I can understand, `bigObject[key]` => `bigObject['a' | 'b']` => `number | string`, if `smallOne[key]` wants to assign to `bigObject[key]`, it must be `string & numer` => never
objectKyes(smallOne).forEach(key => bigObject[key] = smallOne[key]);
// Property 'c' is missing in type '{ a: number; b: string; }' but required in type '{ a: number; b: string; c: number[]; }'.
// Here I can't understand, the type T must be either 'a' or 'b', so does it need attribute 'c'
objectKyes(smallOne).forEach(<T extends keyof (typeof smallOne),>(key: T) => bigObject[key] = smallOne[key]);
</code>
const objectKyes = <T extends {},>(obj: T) => Object.keys(obj) as Array<keyof T>;
const bigObject = {
a: 1,
b: '2',
c: [3],
};
const smallOne = {
a: 1,
b: '2',
} satisfies Partial<typeof bigObject>;
// same as Object.assign(bigObject, smallOne)
// Type 'string | number' is not assignable to type 'never'.
// I can understand, `bigObject[key]` => `bigObject['a' | 'b']` => `number | string`, if `smallOne[key]` wants to assign to `bigObject[key]`, it must be `string & numer` => never
objectKyes(smallOne).forEach(key => bigObject[key] = smallOne[key]);
// Property 'c' is missing in type '{ a: number; b: string; }' but required in type '{ a: number; b: string; c: number[]; }'.
// Here I can't understand, the type T must be either 'a' or 'b', so does it need attribute 'c'
objectKyes(smallOne).forEach(<T extends keyof (typeof smallOne),>(key: T) => bigObject[key] = smallOne[key]);
I am confused on the last line, why TS need attribute ‘c’ in smallone
?
I expect objectKyes(smallOne).forEach(<T extends keyof (typeof smallOne),>(key: T) => bigObject[key] = smallOne[key]);
can work properly, and do not throw error.