In my file, I have the following discriminated union type
interface SlackStageQuery {
stage: 'slack';
}
interface GithubStageQuery {
stage: 'github';
id: string;
}
interface SuccessStageQuery {
stage: 'success';
name: string;
link: string;
}
type StageQuery = SlackStageQuery | GithubStageQuery | SuccessStageQuery;
Additionally, I have the following interface:
interface Details<S extends OnboardingStage> {
position: number;
title: S extends 'success' ? (name: string) => string : string;
subtitle: string;
button: (query: StageQuery & { stage: S }) => JSX.Element;
}
Then, I have an arbitrary object ONBOARDING_STAGE_DETAILS
which conforms to { [p in OnboardingStage]: Details<p> }
.
I also have a query
variable of type StageQuery
.
To get the button
property from one of the sub-objects of ONBOARDING_STAGE_DETAILS
, I do
const foo = ONBOARDING_STAGE_DETAILS[query.stage].button(query)
For some reason however, I get this type error:
TS2345: Argument of type 'StageQuery' is not assignable to parameter of type 'never'. Type 'SlackStageQuery' is not assignable to type 'never'.
I'm not sure why this is, since, for example, if query.stage === 'slack'
, then ONBOARDING_STAGE_DETAILS[query.stage]
would be of type Details<'slack'>
, whose button
property takes a StageQuery & { stage: 'slack' }
, which it is being provided. So I would think all the types would be able to work.
Anyone know why this is and how I can fix it? Thanks!