The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
#Generic inference by argument
10 messages · Page 1 of 1 (latest)
@cinder condor Here's a shortened URL of your playground link! You can remove the full link from your message.
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)
...```
You can choose specific lines to embed by selecting them before copying the link.
You can't have a set of type params where some are inferred and some are manually specified
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
you can somewhat-goofily split it into two parameter lists using a higher-order function:
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) =>
...```
You can choose specific lines to embed by selecting them before copying the link.
I was thinking that would be the case - a bit annoying but ah well, cheers