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

With two (or more) optional type parameters, when the second type argument depends on the first, and the function is called with one type argument, the second is improperly inferred. #57052

Closed
alex-eliot opened this issue Jan 14, 2024 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@alex-eliot
Copy link

🔎 Search Terms

first type argument second type argument optional cannot be inferred
optional type argument wrongly inferred
when first type argument is passed, second one is not inferred

🕗 Version & Regression Information

Occurs on 5.4.0

⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.4.0-dev.20240113#code/GYVwdgxgLglg9mABBATgUwIZTQeQEYBWa0APAFCKIAqiaAHtmACYDOiLUKMYA5ogLztO3HgBoKiHLQZpmbAErE4KJiSqjEACgCUAgHyIMYAJ4HBiiMtXqtu-gaOmyBzQGs0xlgC5qAbQC6GnCEPji6AN4S6FAgKEjBBGQAvmRklmAciOhsgqiY2PhE0Jq+AOTAcHClGqV4GCilgYiRlBVwPjr6iKUYLEzVEnUoHXYGPX2lydqpAPQzyAiZ2T6Rc5SIbSNdHFy8ANxka5RDW-ZCuzwHaylpi1BZaCwATALI6Fi4hMRQJOWVpYgAD7dIalPQlP5VGqgpotDaVU5jXr9cTHeqI7rIyZJaaHebpJaPJ4+CxWEgAIja5KBiHJQ3JGk6Z0ceiAA

💻 Code

function createObject<
  T extends string = string,
  O extends Record<T, () => any> = Record<T, () => any>
> (keys: T[], obj: O) {
  return obj
}

const res = createObject(['foo', 'bar'], {
  foo: () => 'asd',
  bar: () => 'asd'
})

// const res: {
//   foo: () => string;
//   bar: () => string;
// }

const res2 = createObject<'foo' | 'bar'>(['foo', 'bar'], {
  foo: () => 'asd',
  bar: () => 'asd'
})

// const res2: Record<"foo" | "bar", () => any>

🙁 Actual behavior

When the first type argument is passed, the second one is not inferred properly.

🙂 Expected behavior

The inferrence of the two code examples should be identical.

Additional information about the issue

No response

@MartinJohns
Copy link
Contributor

This is working as intended. Type argument inference is all or nothing. You want #26349.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jan 16, 2024
@typescript-bot
Copy link
Collaborator

This issue has been marked as "Duplicate" 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 Jan 19, 2024
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

4 participants