nestjs / mapped-types

Configuration module for Nest framework (node.js) 🐺
https://nestjs.com
MIT License
375 stars 58 forks source link

`RemoveFieldsWithType`: `Omit` instead of `Exclude`? #1376

Closed brkaisin closed 1 week ago

brkaisin commented 6 months ago

Is there an existing issue for this?

Current behavior

In lib/types/remove-fields-with-type.type.ts, we have:

type KeysWithType<T, Type> = {
  [K in keyof T]: T[K] extends Type ? K : never;
}[keyof T];

export type RemoveFieldsWithType<T, Type> = Exclude<T, KeysWithType<T, Type>>;

I suspect that Omit should be used instead of Exclude in type RemoveFieldsWithType because Exclude<T, U> “excludes from T those types that are assignable to U”, whereas type Omit<T, K extends keyof any> “constructs a type with the properties of T except for those in type K”, which seems more what we want here.

You can see in the minimum reproduction code linked below a scenario where the type does not behaves correctly. Correct me if I've missed anything :).

Minimum reproduction code

https://gist.github.com/brkaisin/a305464bb3e3a25230a51329a929bdcc

Steps to reproduce

Just having the NestJS mapped types dependency.

Expected behavior

I think we should not have the error we see in the the minimum reproduction code.

Package version

2.0.5

Node.js version

21.7.1

In which operating systems have you tested?

Other

No response

micalevisk commented 6 months ago

can you see if changing that will change the behavior shown at #1126 ?