Skip to content

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

Closed
@stockson

Description

@stockson

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

Activity

whzx5byb

whzx5byb commented on May 15, 2021

@whzx5byb
MartinJohns

MartinJohns commented on May 15, 2021

@MartinJohns
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.
IllusionMH

IllusionMH commented on May 17, 2021

@IllusionMH
Contributor

Looks like duplicate of #33014

RyanCavanaugh

RyanCavanaugh commented on May 20, 2021

@RyanCavanaugh
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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @stockson@IllusionMH@MartinJohns@whzx5byb@RyanCavanaugh

        Issue actions

          Typescript unable to infer property type with utility type's index signature Β· Issue #44102 Β· microsoft/TypeScript