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

Not valid when inferring return value types from function parameters of nested array types #49296

Closed
fxioi opened this issue May 29, 2022 · 3 comments
Labels
Needs More Info The issue still hasn't been fully clarified

Comments

@fxioi
Copy link

fxioi commented May 29, 2022

Bug Report

A function that passes in a custom schema that constrains the values of the properties of the returned object.

But when the parameters are nested arrays, the type constraint does not take effect.

🔎 Search Terms

nested array, nested tuple, type Inference, type constraints

🕗 Version & Regression Information

v4.6.4

⏯ Playground Link

Playground link with relevant code

💻 Code

function pickFunc<T extends DemoO, U extends ToSchemaType<T>>(
  obj: T,
  schema: U
): PickBySchema<T, U> {
  throw "not yet implemented";
}

const res = pickFunc(post, ["id", "body"]);
res.id;
// The message "title does not exist" was successfully displayed
res.title;
res.body;

// When using nested schema, the type constraint does not take effect
const res = pickFunc(post, ["id", ["tags", ["name"]]]);
res.id;
// Expect an error message
res.title;
// Expect an error message
res.tags[0].id
res.tags[0].name

I created the PickBySchema type to generate attribute field constraints for the object.

It is effective when used alone.

image

type PickBySchema<T extends DemoO, U extends ToSchemaType<T>> = {
  [K in U extends (infer U1)[]
    ? U1 extends string
      ? U1
      : U1 extends [infer U2, SelectSchemaType]
      ? U2 extends string
        ? U2
        : never
      : never
    : never]: T[K] extends string
    ? T[K]
    : T[K] extends (infer O)[]
    ? O extends DemoO
      ? PickBySchema<O, GetNestedSchema<U, K>>[]
      : never
    : never;
};

type PostOnlyId = PickBySchema<Post, ["id", ["tags", ["id", "name"]]]>;

🙁 Actual behavior

When passing nested arrays in function parameters, the return value does not get the expected type constraints

🙂 Expected behavior

When passing a one-dimensional array or a nested array as a function parameter, the return value is expected to be bound by the specified field

@jcalz
Copy link
Contributor

jcalz commented May 29, 2022

That's quite a long reproduction; could you demonstrate the issue with less code?

I think this might be related to #30680; the compiler is inferring wider array/string types than you want.

@RyanCavanaugh RyanCavanaugh added the Needs More Info The issue still hasn't been fully clarified label Jun 1, 2022
@RyanCavanaugh
Copy link
Member

This repro is long enough that it's unlikely to represent a bug in TypeScript. If you can provide a shorter one (5-10 lines is sufficient to reproduce almost any defect) we can investigate, but we don't have the resources to dig in to things this large.

@fxioi
Copy link
Author

fxioi commented Jun 1, 2022

First of all, thank you very much for your replies, and I apologize for providing a slightly longer reproduction. Since this problem was discovered during debugging nested arrays, I'll close the issue before I provide a parsimonious reproduction.

@fxioi fxioi closed this as completed Jun 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs More Info The issue still hasn't been fully clarified
Projects
None yet
Development

No branches or pull requests

3 participants