#How can this partial type not extend its base type?

8 messages · Page 1 of 1 (latest)

desert quartzBOT
#
Dec#1697

Preview:```ts
export 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];
};

type User = {
id: number;
...```

pine patrol
#

I'm assuming it's because the type is too loose?

#

this resolves to true: ```
type IsPartial<TInput> = Partial<TInput> extends TInput ? true : false;

type lol = IsPartial<Partial<MappedEntity<User, UserKeyMap>>>;

stone nexus
#

you have to swap the IsPartial parameters

#

current way is false because number | undefined isnt assignable to number

pine patrol
#

Makes sense, but the problem is I'm doing something like this:

stone nexus
#

or TInput extends Partial<TOf> ? true : false;

pine patrol
#
export interface EntityMapper<TEntity, TKeyMap extends KeyMap<TEntity>>
{
    map<TInput extends TEntity, TOutput extends MappedEntity<TEntity, TKeyMap>>(entity: TInput): TOutput;
    unmap<TInput extends MappedEntity<TEntity, TKeyMap>, TOutput extends TEntity>(mappedEntity: TInput): TOutput;
}

const someEntityMapper: EntityMapper<User, UserKeyMap> = {
    map(entity) { ... },
    unmap(mappedEntity) { ... }
};

// Here I want to say I am mapping it, but to it's concrete (non-partial) variant
// So I pass in MappedEntity<User, UserKeyMap> which _should_ extend Partial<MappedEntity<User, UserKeyMap>>
someEntityMapper.map<User, MappedEntity<User, UserKeyMap>>(user);