type Props = {a: number}
type FuncSig = [string, Props] | [Props]
function thing(...props: FuncSig) {
const {a} = typeof props[0] === 'string' ? props[1] : props[0]
}
In the above code, there should be no way that a
is not known, but the compiler complains:
Property 'a' does not exist on type 'Props | undefined'.
Unfortunately TS doesn't make narrowing by a property's wide type of a union member, but you can narrow by a tuple's length:
type Props = {a: number}
type FuncSig = [string, Props] | [Props]
function thing(...props: FuncSig) {
const {a} = props.length === 2 ? props[1] : props[0]
}