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

Inference of function arguments is inconsitent when reordering object properties #60986

Closed
hayes opened this issue Jan 16, 2025 · 2 comments
Closed
Labels
Duplicate An existing issue was already created

Comments

@hayes
Copy link

hayes commented Jan 16, 2025

🔎 Search Terms

inference infer order function arguments

🕗 Version & Regression Information

This affects all tested version of typescript back to 3.3.3 (oldest version available in the playground)

In 5.0 the started to only affect function using the function keyword, arrow functions and object methods are no-longer affected after 5.0

after 5.0 functions using the function keyword are still affected (functions, generators, and async generators)

⏯ Playground Link

https://www.typescriptlang.org/play/?target=11#code/JYOwLgpgTgZghgYwgAgMoFcBGBnBViYQBiwEANgCYDyADmMAPYjYA8AogG4TgB8yA3gChkybFlz5CALmQAKAJTIAvH07cww5FAjYGZLjNkQu4GWvCKVyEOgC2hKIIC+gwRQgIycbchjoQCPRMyO4woBC0QczsJmA8spoMdIzMMhg4eATEpJSRKazmcQA0gvIyHAzAFK4A9DXI0FAMUFJuEGEgEclM2LJCItq6+hCGxuqWfGPgJSJiGZIQCgJaEGDoUCDIAIwATADMyE4lTvK19Y3NraHheT19moN6BnJTYBMNsTOi4pnSvv6BFJLfgrNYbbb7Q7HU6COrWBgNKBNKDIADuAAtuMgaE0aNAwABPZDNdwo4DYUSouA0PEUK7tG7dZj3WY-BbA0HrTa7A5HB46J4jF6xd6vaFneGI5FozGbdDYUAAc2Q3iaqP+ASivmayAABnMJFldfSOl0or1+ishs8jCLlJNPpoDb8hUsrCDtGDuZC+ScgA

💻 Code

interface SubscribeFieldOptions<Event> {
  subscribe: () => Event
  resolve: (event: Event) => number
}

declare function defineOptions<Event>(
  options: SubscribeFieldOptions<Event>,
): void

defineOptions({
// error: Type 'unknown' is not assignable to type 'number'
  resolve: (event) => event,
  subscribe() { return 123 },
})

// error: Type 'unknown' is not assignable to type 'number'
defineOptions({
  resolve: (event) => event,
  subscribe: function() { return 123 },
})

// no error when property order is swapped:
defineOptions({
  subscribe() { return 123 },
  resolve: (event) => event,
})

// no error when using arrow function for `subscribe`:
defineOptions({
  resolve: (event) => event,
  subscribe: () => { return 123 },
})

🙁 Actual behavior

The event argument is inferred as unknown when using a the resolve method is defined before the subscribe method in the defineOptions call, and resolve is a function, async function, function* or async function*

🙂 Expected behavior

The event argument of the resolve function should be correctly inferred regardless of what kind of function definition is used (arrow, method, function, async function, generator, async generator)

Additional information about the issue

No response

@RyanCavanaugh
Copy link
Member

See #47599

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jan 16, 2025
@hayes
Copy link
Author

hayes commented Jan 16, 2025

Thanks, and sorry for the duplicate. Finding the right issue when you don't know all the right terminology is really hard!

@hayes hayes closed this as completed Jan 16, 2025
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

2 participants