Skip to content

Regression: Tagged types are incorrectly refinedΒ #43502

Closed

Description

@arcanis

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.

Activity

  1. arcanis commented on Apr 2, 2021

    @arcanis
    ContributorAuthor

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

    if ((typeof p) !== `string`)
      throw errors.EBADF(`read`);
  2. ahejlsberg commented on Apr 5, 2021

    @ahejlsberg
    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.

  3. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions