Skip to content

Regression: Tagged types are incorrectly refined #43502

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
arcanis opened this issue Apr 2, 2021 · 2 comments Β· Fixed by #43538
Closed

Regression: Tagged types are incorrectly refined #43502

arcanis opened this issue Apr 2, 2021 · 2 comments Β· Fixed by #43538
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue

Comments

@arcanis
Copy link

arcanis commented Apr 2, 2021

Bug Report

πŸ”Ž Search Terms

refine regression 4.3 refine never

πŸ•— Version & Regression Information

Worked with 4.2, stopped working with 4.3 beta

⏯ Playground Link

https://www.typescriptlang.org/play?ts=4.3.0-dev.20210402#code/C4TwDgpgBAEghgZwBZQLxQcATgSwHYDmUAZFAN4D6FSiSAXFNgK4QC+A3AFCcBmTeAY2A4A9nigEIweMgAyEQsCQAKGsgYykASgZ4mAWwBGELOU5QLUAPRWoAQSjHMUCDx4iswKAKQQBAayh8b0RoESZTPkFhMSCEKCYECAATRxAoSBEwABtoAHdcYUIoACkAZXNLHB4oZVBMmrUUAEJUdAADTFxCdq1Ky0YkLBE8qDwIUYBRLGGsZXaAFSQceOSRCAQ8AHIvbJERQOycf2g4KCbern6oLCkI8SaAOlzFJC5WTiA

πŸ’» Code

type Hash = string & {__hash: true};

function getHashLength(hash: Hash): number {
    // A best effort check in case our function is used by people writing JS
    if (typeof hash !== `string`)
      throw new Error(`This doesn't look like a hash`);

    return hash.length;
}

πŸ™ Actual behavior

The hash variable is incorrectly refined to never.

πŸ™‚ Expected behavior

The hash variable should remain Hash, since the typeof check is coherent with Hash.

@arcanis
Copy link
Author

arcanis commented Apr 2, 2021

An ugly workaround if you don't use Prettier is to do (note the extra parentheses):

if ((typeof p) !== `string`)
  throw errors.EBADF(`read`);

@DanielRosenwasser DanielRosenwasser added this to the TypeScript 4.3.1 milestone Apr 2, 2021
@DanielRosenwasser DanielRosenwasser added the Bug A bug in TypeScript label Apr 2, 2021
@ahejlsberg
Copy link
Member

Hmm, yes this is an effect of #43131. The fixes to getTypeFacts are in principle correct, except that here we think that typeof is both "string" and "object", which is impossible and therefore gets us to never. In tagged primitive types like this we should really consider the primitive type be to dominant. I'll put up a PR to that effect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants