I'm building a TypeScript library that leverages some interfaces from another library. I'm trying to define a type from the intersection of a generic type and an interface I don't control, combined with a union between void, which has special meaning in the dependency library.
I've tried to create a minimal representation of the issue I'm facing.
export type AllProps<Props> = (Props & IDependecyProps) | void;
interface MyProps {
disableCache: boolean;
}
function doTheThing(props: AllProps<MyProps>) {
// Property 'disableCache' does not exist on type 'AllProps'.
// Property 'disableCache' does not exist on type 'void'.ts(2339)
console.log(props.disableCache);
}
My goal is that the AllProps should allow you to specify either disableCache, and any properties in IDependecyProps, OR the type results in void. The library I depend on has a special meaning for void type, which makes it useful.
EDIT: I made the code sample too simple, forgot to add the generic type.
ifwill refine the type and exclude thevoid. Try - Aleksey L.if(props)statement allowed TypeScript to exclude thevoidand assume my type intersection. It works perfectly :) - Andrew Craswell