Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mongodb broken by #41821 #46228

Closed
sandersn opened this issue Oct 5, 2021 · 3 comments
Closed

mongodb broken by #41821 #46228

sandersn opened this issue Oct 5, 2021 · 3 comments
Assignees
Labels
Recent Regression This is a new regression just found in the last major/minor version of TypeScript. Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@sandersn
Copy link
Member

sandersn commented Oct 5, 2021

mongodb has a mapped type SetFields that calls a conditional type Flatten. Before #41821, Flatten propagated its constraint from its argument to its return type. Now it does not.

To reproduce:

  1. npm i mongodb@4.1.2
  2. Create a tsconfig with index.ts and strict: true.
  3. In index.ts, import mongodb = require("mongodb")
  4. Error in mongodb.d.ts on line 5233.

I'll see if I can create a standalone repro.
If this error is intended, we need to provide a fix for mongodb so they don't break when people start using 4.5.

@sandersn sandersn added Needs Investigation This issue needs a team member to investigate its status. Recent Regression This is a new regression just found in the last major/minor version of TypeScript. labels Oct 5, 2021
@sandersn sandersn added this to the TypeScript 4.5.1 milestone Oct 5, 2021
@sandersn
Copy link
Member Author

sandersn commented Oct 5, 2021

Here's a standalone repro (I think you still need strict: true):

type Flatten<Type> = Type extends ReadonlyArray<infer Item> ? Item : Type;
type KeysOfAType<TSchema> = {[key in keyof TSchema]: key}[keyof TSchema];

type OptionalId<TSchema extends {_id?: any;}> = TSchema;
type SetFields<TSchema> = {
    readonly [key in KeysOfAType<TSchema>]?: OptionalId<Flatten<TSchema[key]>>;
}

Errors are on Flatten use inside SetFields.

@weswigham
Copy link
Member

It may be a regression, but it's 100% correct. We should have always issued an error on this, but failed to due to before because we took too long to expand the conditional type constraints (and issued a bogus Maybe result). OptionalId demands that the type you pass it have an optional _id member. Both of Flatten's branches are only constrained to unknown, and thus aren't proven to have that member. You can work around this with the traditional Cast conditional type if you want to, which would look like type Cast<T, U> = T extends U ? T : T & U; and then write OptionalId<Cast<Flatten<TSchema[key]>, {_id?: any}>>. But better would just be removing the constraint on OptionalId's type argument if it's intended to be used on unknown types like this.

@weswigham weswigham added Working as Intended The behavior described is the intended behavior; this is not a bug and removed Needs Investigation This issue needs a team member to investigate its status. labels Oct 13, 2021
@sandersn
Copy link
Member Author

Fixed by mongodb/node-mongodb-native#3004

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Recent Regression This is a new regression just found in the last major/minor version of TypeScript. Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

2 participants