Trying to understand why narrowing the type of the parentEntityMetas
is not working as expected.
The goal is to narrow it down based on the parentEntityType
passed to the function.
I attempted to extract the correct parentEntityMetas type from the SettingsParentEntityTypeMetas using the inferred parentEntityType as an index in the type.
Expecting the condition set on parentEntityType to narrow the type, but it seems to not be working.
Any help or hints would be greatly appreciated.
export type SettingsParentEntityType =
| 'Workspace'
| 'Artist';
export type SettingsParentEntityTypeMetas = {
Workspace: {
workspaceId: string;
};
Artist: {
releaseId: string;
};
};
type Params<
T extends SettingsParentEntityType
> = {
parentEntityType: T;
parentEntityMetas?: SettingsParentEntityTypeMetas[T];
};
const editor = <
T extends SettingsParentEntityType
>({
parentEntityType,
parentEntityMetas,
}: Params<T>) => {
const releaseId = parentEntityType === 'Artist' && parentEntityMetas && parentEntityMetas.releaseId;
}
TS reports an error when attempting to access parentEntityMetas.releaseId
.
Try running the code on TypeScript Playground for more insights: TypeScript Playground