Skip to content

Typescript unable to infer property type with utility type's index signature #44102

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
stockson opened this issue May 15, 2021 · 5 comments
Closed
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@stockson
Copy link

stockson commented May 15, 2021

Bug Report

πŸ”Ž Search Terms

Signature Index with Utility Types

πŸ•— Version & Regression Information

This happened with all versions I've tested including nightly

⏯ Playground Link

I don't have a playground link unfortunately, except my stackoverflow question:
https://stackoverflow.com/questions/67542115/typescript-compiler-failing-with-different-property-types-on-omit-and-index-sign/67542334

πŸ’» Code

interface Obj {
foo: number
bar: string
baz: number
}
type Sub = Omit<Obj, "baz">

const obj: Obj = {foo: 1, bar: "abc", baz: 1}

function fn (key: K, val: Sub[K]) {
obj[key] = val
}

πŸ™ Actual behavior

Typescript should(?) have inferred the data type like it did without the utility type, but is requiring that data types be uniform on the utility type.

πŸ™‚ Expected behavior

The compiler to still be able to compute the data type

@whzx5byb
Copy link

See #31694 (comment)

@MartinJohns
Copy link
Contributor

I don't have a playground link unfortunately

  1. Go to the playground: https://www.typescriptlang.org/play
  2. Insert your code.
  3. Click on "Share".
  4. Paste the URL that has been copied to your clipboard here.

@stockson
Copy link
Author

stockson commented May 15, 2021

@IllusionMH
Copy link
Contributor

Looks like duplicate of #33014

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label May 20, 2021
@RyanCavanaugh
Copy link
Member

This is the intended behavior, because this is a legal call to your function:

fn(Math.random() > 0.5 ? "foo" : "bar", 42);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

5 participants