In my code, I have a type that consists of different config objects:
type Types =
| { kind: 'typeA', arg1: string }
| { kind: 'typeB', arg1: string, arg2: string }
I also defined a type called InnerType
which extracts just the kind
subtype from the union above:
type InnerType = Types['kind'];
This results in innerType
being a union of 'typeA'|'typeb'
.
Furthermore, I created a conditional type to extract only the non-kind
subtypes from the Types
union:
type ExtractOtherParams<K, T> = K extends { kind: T }
? Omit<K, 'kind'>
: never
So far so good - when using this conditional type with a new type like Test
, it correctly returns an object containing only the specified parameters. For example:
type Test = ExtractOtherParams<Types, 'typeA'> // test = { arg1: string }
When passing typeB
to the conditional type, it provides an object with both arg1
and arg2
properties.
type Test = ExtractOtherParams<Types, 'typeB'> // test = { arg1: string, arg2: string }
However, there seems to be an issue when defining a function that utilizes this conditional type. When trying to access others.arg2
within one of the cases, it leads to an error message.
Despite this error, when utilizing the function from a consumer perspective, it functions as expected. The function enforces the correct parameters based on the provided type argument.
What am I missing from the function definition to allow access to arg2
inside the second case expression?
I experimented by defining the function without extending and solely using a generic type, but did not see any changes in behavior. Any suggestions would be appreciated.