help why bar any but not foo

11 Replies
chmod
chmodOP2y ago
type AnyFunc = (...args: any[]) => any;
function proxy<T extends AnyFunc>(fn: T) {
return fn;
}
const nowBarIsAny = proxy((foo: string, bar = 23) => {});
// becomes ^? (foo: string, bar?: any) => void
// instead should be (foo: string, bar?: number) => void
type AnyFunc = (...args: any[]) => any;
function proxy<T extends AnyFunc>(fn: T) {
return fn;
}
const nowBarIsAny = proxy((foo: string, bar = 23) => {});
// becomes ^? (foo: string, bar?: any) => void
// instead should be (foo: string, bar?: number) => void
why does default bar become any, but foo works?
whatplan
whatplan2y ago
function proxy<T extends Function>(fn: T): T {
return fn;
}
const nowBarIsAny = proxy((foo: string, bar = 23) => {});
// ^? (foo: string, bar?: number) => void
function proxy<T extends Function>(fn: T): T {
return fn;
}
const nowBarIsAny = proxy((foo: string, bar = 23) => {});
// ^? (foo: string, bar?: number) => void
chmod
chmodOP2y ago
thanks that worked! however now
function proxy<T extends AnyFunc>(fn: T) {
type Params = Parameters<T>; // can't do this anymore
return fn;
}
function proxy<T extends AnyFunc>(fn: T) {
type Params = Parameters<T>; // can't do this anymore
return fn;
}
fails with
Type 'Function' is not assignable to type '(...args: any) => any'
Type 'Function' is not assignable to type '(...args: any) => any'
whatplan
whatplan2y ago
hm yea this is weird
type MyFunction<T extends (...args: any[]) => any> = T extends (...args: infer A) => infer R
? (...args: A) => R
: never;

function proxy<T extends (...args: any[]) => any>(fn: T): MyFunction<T> {
return fn as unknown as MyFunction<T>;
}

const nowBarIsAny = proxy((foo: string, bar: number = 23) => {});
type MyFunction<T extends (...args: any[]) => any> = T extends (...args: infer A) => infer R
? (...args: A) => R
: never;

function proxy<T extends (...args: any[]) => any>(fn: T): MyFunction<T> {
return fn as unknown as MyFunction<T>;
}

const nowBarIsAny = proxy((foo: string, bar: number = 23) => {});
this is something but not probably the right solution
chmod
chmodOP2y ago
oof still any really don't want to poison all references with generics but it seems it's inevitable
whatplan
whatplan2y ago
what is your use case?
chmod
chmodOP2y ago
a mini trpc
type AnyFn<CTX = void> = CTX extends void ? (...args: any[]) => any : (ctx: CTX, ...args: any[]) => any;
type GenId<Fn extends AnyFn, CTX> = CTX extends void
? (...args: Parameters<Fn>) => string
: OmitFirstArg<(...args: Parameters<Fn>) => string>;

type BaseRouter<CTX> = {
[key: string]: BaseRouter<CTX> | AnyFn<CTX>;
};

type ReplaceAnyFnWithGenId<T, CTX> = {
[K in keyof T]: T[K] extends AnyFn ? GenId<T[K], CTX> : T[K] extends BaseRouter<CTX> ? ReplaceAnyFnWithGenId<T[K], CTX> : never;
};
type AnyFn<CTX = void> = CTX extends void ? (...args: any[]) => any : (ctx: CTX, ...args: any[]) => any;
type GenId<Fn extends AnyFn, CTX> = CTX extends void
? (...args: Parameters<Fn>) => string
: OmitFirstArg<(...args: Parameters<Fn>) => string>;

type BaseRouter<CTX> = {
[key: string]: BaseRouter<CTX> | AnyFn<CTX>;
};

type ReplaceAnyFnWithGenId<T, CTX> = {
[K in keyof T]: T[K] extends AnyFn ? GenId<T[K], CTX> : T[K] extends BaseRouter<CTX> ? ReplaceAnyFnWithGenId<T[K], CTX> : never;
};
seems impossible without having BaseRouter be a mapped type itself sorta like
type BaseRouter<T = AnyFn> = {
[K in keyof T]: T[K] extends AnyFn
? T[K]
: T[K] extends Record<string, any>
? BaseRouter<T[K]>
: never;
};
type BaseRouter<T = AnyFn> = {
[K in keyof T]: T[K] extends AnyFn
? T[K]
: T[K] extends Record<string, any>
? BaseRouter<T[K]>
: never;
};
whatplan
whatplan2y ago
ok gimme a sec to look through this Im out of ideas maybe someone it matts discord can help https://discord.gg/BT5NW6W4
chmod
chmodOP2y ago
thanks for the efforts, I've been at this for several days (and yeah already there lol https://discord.com/channels/997886693233393714/1129809460966207558)
whatplan
whatplan2y ago
maybe take a look at the trpc repo might be helpful
chmod
chmodOP2y ago
have it on the side too and it's pure sorcery too lol, but looks like mapping
type BaseRouter<CTX, T = AnyFn<CTX>> = {
[K in keyof T]: BaseRouter<CTX> | AnyFn<CTX>;
};
type BaseRouter<CTX, T = AnyFn<CTX>> = {
[K in keyof T]: BaseRouter<CTX> | AnyFn<CTX>;
};
annoyingly finally solved nvm loses type enforcement for BaseRouter
Want results from more Discord servers?
Add your server