Narrowing to `never` with `in` in non-union contexts with feature-tested properties



Bug Report

πŸ”Ž Search Terms

never in narrow

πŸ•— Version & Regression Information

  • This changed between ~versions 4.7 and 4.8~ versions 4.8 and 4.9

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

export function generateUID() {
    if ('randomUUID' in crypto) {
        return crypto.randomUUID();

    // Error here, `crypto` is `never`
    return crypto.getRandomValues(new Uint8Array(16));

πŸ™ Actual behavior

Error at indicated line - crypto is of type never

πŸ™‚ Expected behavior

Shouldn't be an error?


randomUUID is a method available in secure contexts only. In this sense it's "optional" in that it may or may not be present, but in another sense it's not optional because any piece of code might willingly take a hard dependency on only running in contexts where this is supported. For this code snippet, which polyfills its own behavior when randomUUID isn't present, the bottom codepath is definitely reachable.

2022-10-04 19:31:26

Add a Comment

Top 3 Comments

  fatcerberus answered on 2022-10-04 19:47:40

On an unrelated note why does everything need a secure context now? You can't even use ES modules without a secure context, which makes no sense. It makes it a pain to test things locally.

0 positive reactions.
  fatcerberus answered on 2022-10-04 19:41:37

0 positive reactions.
  DanielRosenwasser answered on 2022-10-04 19:41:26

It seems bad given that you can do a check for

if (crypto.randomUUID) {
else {
  // not never here, right?

without getting never in the else branch.

(playground link)

0 positive reactions.

Quick Hint

Can we convert TypeScript to JavaScript?

It is possible to have mixed TypeScript and JavaScript projects. To enable JavaScript inside a TypeScript project, you can set the allowJs property to true in the tsconfig. json .

Repo Information

Age 8yrs
Vendor microsoft
Repo Name TypeScript
Primary Language TypeScript
Default Branch main
Last Update 9 hours ago

Microsoft's Code Library

Similar Issues

πŸ’Ύ DirectXShaderCompiler Update Linux build and test workflows to use lit πŸ’¬ 3 open πŸ—“οΈ 16 hours ago
πŸ’Ύ DirectXShaderCompiler DXC API fails to report compilation error πŸ’¬ 3 closed πŸ—“οΈ 1 day ago
πŸ’Ύ vue-styleguidist fix(deps): update typescript-eslint monorepo to v5.45.0 πŸ’¬ 5 closed πŸ—“οΈ 1 day ago
πŸ’Ύ DirectXShaderCompiler [NFC] Address some low hanging fruit UBSan failures πŸ’¬ 5 open πŸ—“οΈ 1 day ago
πŸ’Ύ hash Update TypeScript to 4.9 πŸ’¬ 4 closed πŸ—“οΈ 2 days ago
πŸ’Ύ AzureTRE Amend conda configuration in Nexus πŸ’¬ 3 open πŸ—“οΈ 2 days ago
πŸ’Ύ AzureTRE Firewall policies and Ip group πŸ’¬ 3 open πŸ—“οΈ 2 days ago