#Typing for empty object resulting from a fancy generic

41 messages · Page 1 of 1 (latest)

hushed rivetBOT
#
balam314#0

Preview:```ts
/** Returns the type for an arg type string. Example: returns number for "time". */
export type TypeOfArgType<T> =
T extends "string" ? string :
T extends "boolean" ? boolean :
T extends "number" ? number :
T extends "time" ? number :
never;

export type U2I<U> = (
...```

ocean olive
#

a function expecting an empty object
a function shouldn't expect an object to be explicitly empty, it would instead not expect there to be any properties, which would be {}. Record<string, never> isn't very useful in any capacity

wooden gull
#

Yep, i need it to be something other than Record<string, never> because thats wrong

#

I tried using object

#

but then I get a weird error

ocean olive
#

could you provide some context as to what you're trying to achieve here

wooden gull
ocean olive
#

it's kinda hard to get a comprehensive overview from the scattered comments in the playground

wooden gull
#

ok so, at the high level, it's this

#

this is a bit of the code that allows that to work

ocean olive
#

and what's the specific thing you're trying to get work here?

wooden gull
#

I have functions that accepts the data passed to a handler function

#

which varies based on the parameters for that command

#

some of these functions require args of a specific name

#

this worked fine

#

Req.moderate() returns a function that accepts the arguments passed through the handler, and maybe throws an error

#

this one depends on the arguments object, but there are others that don't care about the arguments object and will work if the arguments object is empty

#

(null! as CommandHandlerData<"map:string">).args;
//                                           ^?
(null! as CommandHandlerData<never>).args;
//                                   ^?
((y:CommandHandlerData<never>) => {})(null! as CommandHandlerData<"map:string">);
//Type '{ map: string; }' is not assignable to type 'Record<string, never>'
//I should be able to pass an object with data to a function expecting an empty object
((y:Record<string, never>) => {})({map: "hi"}); //nope
((y:object) => {})({map: "hi"}); //this works
#

what's happening there is

#

I have the arguments object for a command with arbitrary arguments

#

and want to pass that to a function that accepts any kind of argument object

tepid thicket
#

The easiest way to fix it is to get rid of U2I:

hushed rivetBOT
#
nonspicyburrito#0

Preview:ts /** Returns the type for an arg type string. Example: returns `number` for "time". */ export type TypeOfArgType<T> = T extends 'string' ? string : T extends 'boolean' ? boolean : T extends 'number' ? number : T extends 'time' ...

ocean olive
#

so CommandHandlerData<"map:string"> needs to be a subtype of CommandHandlerData<never>?

tepid thicket
#

But I would personally write it in a very different way.

wooden gull
wooden gull
ocean olive
tepid thicket
#

Here's how I would write it:

hushed rivetBOT
#
nonspicyburrito#0

Preview:```ts
type TypeMap = {
string: string
boolean: boolean
number: number
time: number
}

type ToType<T> = TypeMap[T & keyof TypeMap]

type ExtractName<T> = T extends ${infer N}:${string}
? N
: never

type ExtractType<T> = T extends `${string}
...```

tepid thicket
#

With this solution, you don't need to check for never explicitly, it just works.

wooden gull
#
Argument of type 'CommandHandlerData<"map:string">' is not assignable to parameter of type 'CommandHandlerData<never>'.
  Type 'string' is not assignable to type 'never'.

which doesn't make sense, why is it trying to compare "string" with "never"?

wooden gull
tepid thicket
#

!hb mapped

hushed rivetBOT
tepid thicket
#

It's the "as remapping" section.

wooden gull
#

this fixed my issue, thank you very much