Closed as not planned
Description
π Search Terms
none
π Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________
β― Playground Link
π» Code
function foo<Data extends Record<string, any>>(data: Data) {
const BASE_DATA = {
"DELETED": true
};
// any merging method...
// https://stackoverflow.com/questions/49682569/typescript-merge-object-types
type RET_TYPE = Omit<Data, "DELETED"> & typeof BASE_DATA;
let result = Object.assign({}, data, BASE_DATA);
let result2 = Object.assign({}, BASE_DATA, data);
let result3 = {} as RET_TYPE;
type T1 = RET_TYPE["DELETED"]; // = RET_TYPE["DELETED"]...
type T2 = typeof result.DELETED; // boolean....
type T3 = typeof result2.DELETED; // boolean.... wrong...
type T4 = typeof result3.DELETED; // boolean.... so you know it...
type T5 = typeof result3["DELETED"]; // = {...}["DELETED"]
type RET_TYPE2 = Omit< Record<string, any>, "DELETED"> & typeof BASE_DATA;
type T6 = RET_TYPE2["DELETED"]; // boolean...
let i3: T1 = true; // error
let i1: T5 = true; // error
let i2: T6 = true; // ok
}
π Actual behavior
In this context, TS is somehow losing type informations when manipulating types with [""]
(cf T1 and T5), when he knows the type as shown by T4. In other contexts, TS doesn't lose the type informations as shown by T6.
There is also an issue with Object.assign()
where the returned type is wrong (cf T3, data
can have a member called DELETED
that will override BASE_DATA.DELETED
in the Object.assign()
.)
π Expected behavior
TS shouldn't lose type informations and shouldn't bully me like that.
T3 should be any, as data
can have a member called DELETED
that will override BASE_DATA.DELETED
in the Object.assign()
.
Additional information about the issue
No response