microsoft / microsoft/TypeScript

Disallow values of type 'symbol' from being passed to 'Number' function/constructor or 'String' constructor

Open
#32,895 9 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

Suggestion

Change the NumberConstructor and StringConstructor interfaces to disallow passing values of type Symbol. Currently it accepts values of type any.

Attempting to call Number(Symbol()), new Number(Symbol()), or new String(Symbol()) all throw TypeErrors at runtime.

Note: String(Symbol()) is fine - only new String(Symbol()) will throw.

Use Cases

Any time a value of type any is passed to Number, there is a risk of that value being a symbol and throwing a TypeError.

Object keys are explicitly allowed to be of type symbol (as number | string | symbol). Passing an arbitrary object key to Number can throw a TypeError as well.

Examples

Checking if a value can be coerced to a number

I just ran into this issue when checking if an object key could be a valid array index. The following should be an error as it will throw if value is of type symbol.

function keyCanBeArrayIndex(value: string | number | symbol): boolean {
  return !Number.isNaN(Number(value));
}

Having this be invalid would have forced me to write something like:

function canBeArrayIndex(value: string | number | symbol): boolean {
  try {
    return !Number.isNaN(Number(value));
  } catch {
    return false;
  }
}

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

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 by locating the NumberConstructor and StringConstructor interface declarations in TypeScript's library definitions and review existing tests for constructor argument checking. Confirm the desired distinction between Number(Symbol()), new Number(Symbol()), String(Symbol()), and new String(Symbol()), then add coverage showing the intended compile-time diagnostics. Done means symbol values are rejected only where the issue specifies, without changing emitted JavaScript.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
compilers
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.