For the following example:
type Base = {
base : string,
count : number
}
type ExtendedA = Base & {
special : string
}
type ExtendedB = Base & {
hidden : number
}
function EXAMPLE (payload : ExtendedA | ExtendedB) {
if ("special" in payload) {
payload.special
}
else {
payload.hidden
}
}
const payload : Parameters<typeof EXAMPLE>[0] = {
hidden : 123,
}
How do I ensure that special
isn't also shown/allowed, since I have chosen hidden
as one of my properties, which only exists in ExtendedB
and not ExtendedA
Extended
With a more practical example with StrictUnion, it fails when trying to pass onto other functions
type Base = {
base : string,
count : number
}
type ExtendedA = Base & {
special : string
}
type ExtendedB = Base & {
hidden : number
}
type UnionKeys<T> = T extends T ? keyof T : never;
type StrictUnionHelper<T, TAll> =
T extends any
? T & Partial<Record<Exclude<UnionKeys<TAll>, keyof T>, never>> : never;
type StrictUnion<T> = StrictUnionHelper<T, T>
function SPECIAL (payload : ExtendedA) {
}
function HIDDEN (payload : ExtendedB) {
}
function EXAMPLE (payload : StrictUnion<ExtendedA | ExtendedB>) {
if ("special" in payload) {
SPECIAL(payload)
}
else {
HIDDEN(payload)
}
}
const payload : Parameters<typeof EXAMPLE>[0] = {
hidden : 123,
base : "abc",
count : 2,
}
EXAMPLE(payload)