#Generic inference by argument

10 messages · Page 1 of 1 (latest)

thick sandBOT
#

@cinder condor Here's a shortened URL of your playground link! You can remove the full link from your message.

Dec#1697

Preview:```ts
type KeyMap<TEntity> = {
[Key in keyof TEntity]?: string;
};

export type MappedEntity<TEntity, TKeyMap extends KeyMap<TEntity>> = {
[Key in keyof TEntity as TKeyMap[Key] extends string ? TKeyMap[Key] : Key]: TEntity[Key];
};

function inverseKeyMap<TEntity, const TKeyMap extends KeyMap<TEntity>>(keyMap: TKeyMap)
...```

toxic pier
#

You can't have a set of type params where some are inferred and some are manually specified

cinder condor
#

that seems pretty silly sigh cheers, was hoping there was a way around it

#

seems annoying as it has the type via the argument

#

which makes that second case impossible as I'd have to somehow get the typeof it

green moat
#

you can somewhat-goofily split it into two parameter lists using a higher-order function:

thick sandBOT
#
mkantor#7432

Preview:```ts
type KeyMap<TEntity> = {
[Key in keyof TEntity]?: string;
};

export type MappedEntity<TEntity, TKeyMap extends KeyMap<TEntity>> = {
[Key in keyof TEntity as TKeyMap[Key] extends string ? TKeyMap[Key] : Key]: TEntity[Key];
};

const inverseKeyMap = <TEntity>() => <const TKeyMap extends KeyMap<TEntity>>(keyMap: TKeyMap) =>
...```

cinder condor
#

I was thinking that would be the case - a bit annoying but ah well, cheers