Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TS Mapped Type: Conditionally remove certain key base on value

Tags:

typescript

I'm looking for a solution to produce a mapped type that omit certain key base on the mapped key's value.

Concrete example:

I have an option: Options struct, where Options is an union type of shape:

{ type: DataType } or { type: DataType, params: DataParam }

Some elements of the union have params key and some don't. I want to produce such union utilizing mapped type. Currently I have this working solution (Playground Link):

type DataParams = {
  trade: never;
  trade_bar: {
    interval: number;
    intervalUnit: "ms" | "s" | "m" | "ticks" | "vol";
  };
};


type DataTypes = keyof DataParams;

type OptionsForDataType<T extends DataTypes> = {
  type: T;
} & (DataParams[T] extends never
  ? {}
  : {
      params: DataParams[T];
    })


type Options = {
  [DataType in DataTypes]: OptionsForDataType<DataType>;
}[DataTypes];

But I'm not entirely satisfied with the ugly intersection + extends trick. Is there a better way to express such intention?

I was under the (probably wrong) impression that when { key: never }, that key is effectively excluded from the struct, since nothing is subtype to never, nothing can be assigned to that key. Turns out this is not the case, which looks strange to me.

like image 212
hackape Avatar asked Jun 07 '26 10:06

hackape


2 Answers

I'm kinda embarrassed that I didn't do my Google Diligence™️ thoroughly. Search "mapped type exclude never value" keywords and it takes me to microsoft/TypeScript#23199, where people has the same doubt post the issue and got a solution.

So the digest to that issue thread is, my doubt is legit, the TS team also think { key: never } should exclude the key, however due to some technical difficulty they cannot implement it.

Workaround is to create a utility type that filters desired keys, and apply it as an intermediate step. Solution (Playground Link), taken from that issue and adapted to my use case, goes like:

// Utilities:
type FilteredKeys<T> = { [P in keyof T]: T[P] extends never ? never : P }[keyof T];
type ExcludeNeverFields<O> = {
  [K in FilteredKeys<O>]: O[K]
}

// Use case:
type DataParams = {
  trade: never;
  trade_bar: {
    interval: number;
    intervalUnit: "ms" | "s" | "m" | "ticks" | "vol";
  };
};

type DataTypes = keyof DataParams;

type Options = {
  [DataType in DataTypes]: ExcludeNeverFields<{
    type: DataType;
    params: DataParams[DataType];
  }>
}[DataTypes];
like image 122
hackape Avatar answered Jun 10 '26 17:06

hackape


As of TypeScript 4.1, you can use Key Remapping to remove certain keys based on a conditional type.

The conditional type must be used to remap the key. The key will be excluded if the conditional type returns never.

type RemoveCertainKey<T> = {
  [K in keyof T as CONDITION]: T[K]
}

// CONDITION is a conditional type that can return either the original key,
// a rewritten key, or never if you want to exclude this property

Here's a playground link to demonstrate how it works. As you can see, condition can be based either on the key or on the value to exclude or keep certain properties.

like image 24
Romain Durand Avatar answered Jun 10 '26 19:06

Romain Durand