I have this TypeScript code:
type MyType1 = {
prop1: number,
prop2: string,
prop3: string
}
type MyType2 = {
prop1: number,
prop2: string
}
const objOfType1 = { prop1: 1, prop2: "hello world", prop3: "should be removed" } as MyType1;
console.log('objOfType1', objOfType1);
const objOfType2 = objOfType1 as MyType2;
console.log('objOfType2', objOfType2);
const objOfType2Copy = { ...objOfType2 };
console.log('objOfType2Copy', objOfType2Copy);
The output in all 3 cases is the same
objOfType1, objOfType2, and objOfType2Copy all have the same 3 properties. I was expecting that output for both objOfType2 and objOfType2Copy won't contain prop3 property.
How can I achieve the "expected" behavior?