Amidst the changes in Typescript 4.9, there are some updates to how the in
operator operates.
Given the code snippet below, what approach can I take to convince tsc that id
is not just a primitive value?
export type KeyOrId =
| {
key: string;
}
| {
id: string;
};
export default function checkValue<K extends KeyOrId | null>(
value: K,
) {
if (!value) {
return undefined;
}
// The error 'NonNullable<K>' may refer to a primitive data type and cannot be used with the 'in' operator.(2638)
return 'id' in value;
}
I attempted using a guard like typeof value !== 'object'
, but it didn't work out as expected.