microsoft / microsoft/TypeScript

unknown types should not able to concat with strings

Open
#29,963 3 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

TypeScript Version: 3.3.0

Code

declare const a: unknown;
const b = a + 123;
const c = a + '123';
const d = `${a}123`;

Expected behavior:
All the declarations of variables b c d should have errors.

Actual behavior:
Only declaration of variable b have error.

Playground Link:
https://www.typescriptlang.org/play/index.html#src=declare%20const%20a%3A%20unknown%3B%0Aconst%20b%20%3D%20a%20%2B%20123%3B%0Aconst%20c%20%3D%20a%20%2B%20'123'%3B%0Aconst%20d%20%3D%20%60%24%7Ba%7D123%60%3B%0A

Although we won't get runtime errors in this case, it may cause unpredictable result, e.g. "[[Object]]123", which is unexpected, so I think ts should not allow unknown types to concat with strings.

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 TypeScript Playground reproduction and compare the diagnostics for b, c, and d when a is typed as unknown. Trace the type-checking behavior for arithmetic, string concatenation, and template expressions. Done means the behavior matches the stated expected errors and the relevant checks are covered.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.