I've noticed the following behavior in typescript that seems like a bug. If it is not, what is the reason aliasing the type changes things?
type Item = { id: number };
type Container<T extends Record<string, Item>> = {
name: string,
items: T,
pickedItemKeys?: (keyof T)[],
}
function container<T extends Record<string, Item>>(
options: Container<T>
): Container<T> {
return {
...options
}
}
const a = container({
name: 'test',
items: {
a: { id: 1 },
b: { id: 2 },
},
pickedItemKeys: ['a']
});
// here's the weird part
function consumeContainer(container: Container<Record<string, Item>>) {
Object.entries(container.items).forEach(([k, v]) => console.log(v.id));
}
// does not compile - it's the pickedItemKeys that's setting it off
consumeContainer(a);
// but if I define a type alias for the type used as the argument in the above fn
type UntypedContainer = Container<Record<string, Item>>;
function consumeContainer2(container: UntypedContainer) {
Object.entries(container.items).forEach(([k, v]) => console.log(v.id));
}
// it works fine ¯\_(ツ)_/¯
consumeContainer2(a);