Skip to content

Type assertion does not creates a type union anymore #54883

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
drtex0 opened this issue Jul 4, 2023 · 6 comments
Closed

Type assertion does not creates a type union anymore #54883

drtex0 opened this issue Jul 4, 2023 · 6 comments
Labels
Duplicate An existing issue was already created

Comments

@drtex0
Copy link

drtex0 commented Jul 4, 2023

Bug Report

Since version 5.1.X I am facing this issue with type assertion. In version 5.0.X the type resolves with an Union of the original type & the assertion type. However in 5.1.X there is only the original type left

🔎 Search Terms

🕗 Version & Regression Information

  • This is a crash
  • This changed between versions 5.0.1 and 5.1.3
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about type assertion
  • I was unable to test this on prior versions because _______

⏯ Playground Link

Playground link with relevant code

💻 Code

import { mapValues } from 'lodash';

type Dinero  = {
  mul: () => number
}

const isDinero = (obj: unknown): obj is Dinero => 
  typeof obj === "object" && obj !== null && Object.hasOwn(obj, "mul")

const transformValues = <Contract extends object>(contract : Contract)  => {
   mapValues(contract, (value, key) => {
      if(isDinero(value)) {
        //  value -> Contract[keyof Contract] in TS 5.1.X
        // value: Contract[keyof Contract] & Dinero in TS 5.0.X
        value.mul() 
        
      }

    })

}

🙁 Actual behavior

The type value is Contract[keyof Contract

🙂 Expected behavior

The type value to be at least Contract[keyof Contract] & Dinero

@fatcerberus
Copy link

  1. isDinero is a type predicate, not a type assertion. Type assertion is e.g. as Foo
  2. The expected type is an intersection, not a union.

@thibault-lr
Copy link

thibault-lr commented Jul 5, 2023

Indeed.

  1. isDinero is a type predicate, not a type assertion. Type assertion is e.g. as Foo
  2. The expected type is an intersection, not a union.

Indeed, I'm not allowed to update the post content however.

@RyanCavanaugh
Copy link
Member

Adding a self-contained repro so this can be bisected...

type Dinero  = {
  mul: () => number
}
declare const isDinero: (obj: unknown) => obj is Dinero;
const transformValues = <Contract extends object>(contract : Contract)  => {
    let value: Contract[keyof Contract] = null as any;
    if(isDinero(value)) {
      value.mul() 
    }
}

@typescript-bot
Copy link
Collaborator

The change between origin/release-5.0 and origin/release-5.1 occurred at 84a09c7.

@RyanCavanaugh
Copy link
Member

The originating PR #53059 is being reverted in favor of #54845, so I think it's safe to call this a duplicate

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jul 6, 2023
@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 Jul 9, 2023
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

5 participants