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

Parameters<typeof someFunction> does not preserve type constraint or parameter inference. #54014

Closed
DrSammyD opened this issue Apr 25, 2023 · 5 comments

Comments

@DrSammyD
Copy link

Given the following two types.

type Channel = {
  [key: string]: number;
};

type T<C extends Channel> = {
  channel: C;
  props: (keyof C)[];
};

The following function is able to correctly constrain the type of the second parameter given the following function declaration and call.

function createObject<C extends Channel>(channel: C, props: (keyof C)[]): T<C> {
  return {
    channel,
    props,
  };
}

const someObject = createObject(
  {
    Stuff: 1,
    Yarn: 2,
  },
  ['Stuff', 'Yarn', 'Thing'] // If you try to add a string that is not a key of 'channel', you'll get a type error. Thing is inferred invalid, and I don't have to specify a generic property
);

However, this constraint is not preserved in the Parameters utility type.

let thing: Parameters<typeof createObject> = [
  {
    "Stuff": 1,
    "Yarn": 2,
  },
  ["Yarn", "Stuff", "Thing"] // So... that constraint wasn't preserved.
]

At least not until the generic is explicitly set.

let thingAgain: Parameters<typeof createObject<{"Stuff":number;"Yarn":number}>> = [
  {
    "Stuff": 1,
    "Yarn": 2,
  },
  ["Yarn", "Stuff", "Thing"] // And we're back!
]

Ultimately, the desired functionality is to have types obtain the same type inference as functions without a runtime requirement to keep the typing/code dry.

Playground link

@fatcerberus
Copy link

This would first require implementing a new kind of a generic type that would have its type parameter(s) inferred on assignment, as well as the ability for type aliases to directly accept and return generic types (HKT). Neither of these are possible with the current type system.

@fatcerberus
Copy link

a new kind of a generic type that would have its type parameter(s) inferred on assignment

For this, see #47755.

the ability for type aliases to directly accept and return generic types (HKT)

For HKT, there are several issues, notably #45438, #44875, and the seminal issue #1213.

@DrSammyD
Copy link
Author

DrSammyD commented Apr 25, 2023

Thanks @fatcerberus for linking the other issues.

@DrSammyD
Copy link
Author

DrSammyD commented May 1, 2023

I'm going to reopen this, because even if those other suggestions are implemented, Parameters will probably still be unconstrained.

@DrSammyD DrSammyD reopened this May 1, 2023
@RyanCavanaugh
Copy link
Member

To expedite the triage process, we need everyone to follow the issue template and instructions.

When you clicked "Create New Issue", the issue form was pre-populated with a template and some instructions. We need you to read those instructions completely, follow them, and then fill in all the fields in that template.

We are not able to assist with issues that don't follow the template instructions as they represent a significantly larger amount of work compared to issues which are correctly specified. Thank you for understanding.

@RyanCavanaugh RyanCavanaugh closed this as not planned Won't fix, can't repro, duplicate, stale Jun 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants