Skip to content

spread operator infer to wrong type with method override and generic #59281

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

Closed
syi0808 opened this issue Jul 15, 2024 Β· 8 comments Β· May be fixed by #59290
Closed

spread operator infer to wrong type with method override and generic #59281

syi0808 opened this issue Jul 15, 2024 Β· 8 comments Β· May be fixed by #59290
Labels
Not a Defect This behavior is one of several equally-correct options

Comments

@syi0808
Copy link
Contributor

syi0808 commented Jul 15, 2024 β€’

πŸ”Ž Search Terms

generic spread

πŸ•— Version & Regression Information

  • This changed on version 5.3.0-dev.20231027

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.5.3&ssl=9&ssc=72&pln=10&pc=79#code/C4TwDgpgBAsg9gYwNYDECGDhwE4gOoCWwAFgBIQA2k2APDFALxQCuAdkq3AO6sB8jUABQBYAFBQoBALZgcwAPLYCAcwKs0FAFw0AKlAgAPYBFYATAM6wBMXoICUjfgAVscKQXMRdvMQ4bO0bGACDRoAJQgEHFMaJAgQOAAzWAAaKDRWEF4fUVBIKBc3Dwh4ZHRMHHwiMkpqOgE2Dm4+ARFxSRk5RRU1DU0oXX0jEwsrJht7RwLXd09vXynC2a8nQODQiKjsGLiE5Jg0jKzssTEo1nNgFmCtKABvMQkpRCRvQWfTZgoIfqXi7zSiQwWFwAH5+qVUMDKoQSOQqBBaDpeHZ+gA3OAEUyPKDPZBvD5fH7TIpzZGA6Fg34zYqQ8ogqpw2qI7yoqAYrFiAC+Age7TxSH6gkm-nuXIcaEsR25AG5TqJBJKQKwEFBEmxMAQ4KxJnyJMwbgA6AWCaSyIKCADkYDQJEtdkO5mVqtNnSC3VU6gofn4gj1EighqDiq4aCIHXNCiUno09jsOPFdjlonF9hlQA

πŸ’» Code

type MockFactoryWithHelper<M = unknown> = (
  importOriginal:<T extends M = M>() => Promise<T>
) => Partial<Record<keyof M, any>>
type PromiseMockFactoryWithHelper<M = unknown> = (
  importOriginal: <T extends M = M>() => Promise<T>
) => Promise<Partial<Record<keyof M, any>>>

const util: {
  mock<T>(module: Promise<T>, factory?: MockFactoryWithHelper<T>): void
  mock<T>(module: Promise<T>, factory?: PromiseMockFactoryWithHelper<T>): void
} = {
  mock: (() => {}) as any
};

(async function() {
  util.mock(import('path'), async (importOriginal) => ({
    ...(await importOriginal())
  }));
})();

πŸ™ Actual behavior

Typescirpt throw error like Spread types may only be created from object types.(2698); on line spread syntax

πŸ™‚ Expected behavior

Typescirpt should not throw error on inline spread syntax.

Additional information about the issue

No response

@syi0808 syi0808 changed the title Spread syntax throw error with function override and generic Spread syntax throw error with method override and generic Jul 15, 2024
@RyanCavanaugh
Copy link
Member

The error here is because you're trying to spread a never. This is not a syntax error.

@RyanCavanaugh RyanCavanaugh added the Question An issue which isn't directly actionable in code label Jul 15, 2024
@syi0808
Copy link
Contributor Author

syi0808 commented Jul 15, 2024 β€’

The error here is because you're trying to spread a never. This is not a syntax error.

But it works well on version before 5.3.0-dev.20231027. And return value of importOriginal should never be infer as never type.

That's right. I changed the title.

@syi0808
Copy link
Contributor Author

syi0808 commented Jul 15, 2024

And if this is an unintended bug, I would love to contribute. Where can I find the changes to the 5.3.0-dev.20231027 distribution version?

@syi0808 syi0808 changed the title Spread syntax throw error with method override and generic spread operator infer to wrong type with method override and generic Jul 15, 2024
@RyanCavanaugh RyanCavanaugh removed the Question An issue which isn't directly actionable in code label Jul 15, 2024
@RyanCavanaugh
Copy link
Member

RyanCavanaugh commented Jul 15, 2024 β€’

every-ts can bisect to a specific commit https://github.com/jakebailey/every-ts

@syi0808
Copy link
Contributor Author

syi0808 commented Jul 16, 2024

I'll try it within a week. Thanks for sharing.

@Andarist
Copy link
Contributor

Andarist commented Jul 16, 2024 β€’

Given that overloads are generally picked from the top to the bottom. Shouldn't this be fixed at the definition site of this mock method? If you swap those overloads it works OK: TS playground

Similarly, it works OK if you combine those 2 overloads into a single signature: TS playground. This solution should be preferred over the first one.

@RyanCavanaugh RyanCavanaugh added the Not a Defect This behavior is one of several equally-correct options label Jul 26, 2024
@RyanCavanaugh
Copy link
Member

I agree with @Andarist 's comments in the PR - this doesn't seem like a bug.

@typescript-bot
Copy link
Collaborator

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

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Jul 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Not a Defect This behavior is one of several equally-correct options
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants