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

Argument type infer not work correctly without specify types of function params #47577

Closed
Jokcy opened this issue Jan 24, 2022 · 9 comments
Closed
Labels
Duplicate An existing issue was already created

Comments

@Jokcy
Copy link

Jokcy commented Jan 24, 2022

Bug Report

🔎 Search Terms

  • Argument type infer not work correctly

🕗 Version & Regression Information

seems to be every version of typescript shows this behavior

⏯ Playground Link

Playground Link

In this example, if you try the use commits in actions.a, you will see the type of commits is:

Record<string, (state: {
    name: string;
    arr: never[];
}, ...args: any) => void>

If I update the commit function to use state: State,

...
{
  commits: {
    a: (state: State, newName: string) => 'a'
  }
}
...

then the commits type in actions.a will be exactly what commits is, eg:

{
    a: (state: State, newName: string) => string;
}

🙁 Actual behavior

Without specify state: State, the commits type infer in actions.a not correct. I suppose TS will infer what state is, and there should not be different with/without specify state: State.

🙂 Expected behavior

there should not be different with/without specify state: State.

@jcalz
Copy link
Contributor

jcalz commented Jan 24, 2022

Feels like #38872

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jan 24, 2022
@Jokcy
Copy link
Author

Jokcy commented Jan 26, 2022

@jcalz So in this situation is there any better soluation to make it work?

@jcalz
Copy link
Contributor

jcalz commented Jan 26, 2022

Split the single object into multiple parameters like this? (Note that the specific type named State will not magically be inferred anywhere since you never refer to it anywhere inside the call to defineStore().)

@Jokcy
Copy link
Author

Jokcy commented Jan 26, 2022

@jcalz THX very much, help a lot. Is there any article about context sensitive of typescript? I still not quite understand why the demo you provide solve this issue. I looked at the linked issue (also some issue related by it), I assume (state, newName: string) => 'a' is a context sensitive function and it will not be inferred at phase1 type inference. Or maybe because different args will be inferred in order instead together?

@jcalz
Copy link
Contributor

jcalz commented Jan 26, 2022

A context sensitive function is not fully annotated and so the compiler must use contextual typing to infer something about its type.

According to this comment the compiler performs a phase of inference for every function parameter which is context sensitive, but it does not perform multiple phases of inference for a single function parameter.

@RyanCavanaugh
Copy link
Member

I opened #47599 to track further work in the area

@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@AtLastDopamine
Copy link

像这样将单个对象拆分为多个参数?(请注意,命名的特定类型State不会在任何地方神奇地推断出来,因为您永远不会在调用中的任何地方引用它defineStore()。)

@jcalz Your answer is very helpful, but I still don't understand how splitting into multiple parameters works.

In your example, if I switch the position of 'commits' and 'actions', the type derivation still does not work properly

@jcalz
Copy link
Contributor

jcalz commented Feb 23, 2022

Some inference only works from left-to-right in order of parameters, see this comment for example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

5 participants