microsoft / microsoft/TypeScript

Alias Binding incorrectly propagates through properties on ephemeral import

Open
#43,891 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Bug Report

This is related to the [closed] issue #41628. We have some code that requires JSON (with "resolveJsonModule": true set in our tsconfig.json) and immediately accesses a string field (package version) on the returned JSON object, assigning it to a var/let variable. This produces the following error:

Cannot assign to 'version' because it is not a variable. ts(2539)
🔎 Search Terms

require json cannot assign let alias

🕗 Version & Regression Information
  • This changed between versions 4.0 and 4.1
⏯ Playground Link

Playground link with relevant code

💻 Code
let version = require('./package.json').version;
version += "-debug";
🙁 Actual behavior

TypeScript reports the following error:

Cannot assign to 'version' because it is not a variable.
🙂 Expected behavior

We expect no error in this case. The version property on the object returned by the require function should not be considered an alias, but a simple string.

One workaround is, of course, to use the //ts-ignore hammer. Another is to do something like the following:

const pkg = require('./package.json');
let version = pkg.version;
version += "-debug";

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

Reproduce the Playground example and compare behavior across TypeScript 4.0 and 4.1, focusing on alias binding for a property read from require('./package.json'). No source file or test is named; trace the type-checker path for this ephemeral import and add coverage showing that version can be reassigned without the error.

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
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.