I've tried to apply this solution to disallow call of generic function with second type equal to any
.
Following construction works until the first generic parameter is explicitely specified:
declare function f<V = any, A extends ISmth = ISmth>(a: IfAny<A, never, A>): V;
In case that I need V
to be first parameter and to be optional (it's return type and can't be inferred - it should iether be specified explicitely or be any), how can I fix the code?
I've tried to change default value, but it breakes any calls with explicit first type:
declare function g<V = any, A extends ISmth = any>(a: IfAny<A, never, A>): V;
interface ISmth { x: number; }
type IfAny<T, Y, N> = 0 extends (1 & T) ? Y : N;
declare function f<V = any, A extends ISmth = ISmth>(a: IfAny<A, never, A>): V;
declare function g<V = any, A extends ISmth = any>(a: IfAny<A, never, A>): V;
declare var x: ISmth, y: any;
f(x); // Fine
f(y); // Fine: Argument of type 'any' is not assignable to parameter of type 'never'.
f<string>(x); // Fine
f<string>(y); // BAD: Should be an error, but compiles
g(x); // Fine
g(y); // Fine: Argument of type 'any' is not assignable to parameter of type 'never'.
g<string>(x); // BAD: Should NOT be an error
g<string>(y); // Fine: Argument of type 'any' is not assignable to parameter of type 'never'.