microsoft / microsoft/TypeScript

In Javascript, `typeof x !== undefined ? t : f` should act like a declaration of `x` in the true branch

Open
#22,453 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Domain: JavaScript
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

Search Terms:
existence of define typeof undefined

Code

// @noEmit: true
// @allowJs: true
// @checkJs: true
// @strict: true
// @Filename: a.js
if (typeof define !== 'undefined' && define.amd) {
    define([], function() {
    })
}
// also, nested
exports.storage = 'undefined' != typeof chrome &&
    'undefined' != typeof chrome.storage ?
    chrome.storage.local :
    undefined;

Expected behavior:
The check type define !== undefined should cause the true branch of the conditional to act as if define were declared.

Actual behavior:
Cannot find name 'define'.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the a.js reproduction and its @allowJs, @checkJs, and @strict settings, then run the TypeScript checker against the two typeof patterns. Trace how the checker handles the conditional branches and unresolved names. Done means both examples type-check without a Cannot find name error while preserving the existing behavior for nested checks.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.