microsoft / microsoft/tslib

TypeError when spreadArray is used on a string containing an emoji/Unicode character

Open
#175 1 comment 10 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
1.3k
Forks
145
PR merge metrics
No merged PRs in 30d

Description

Expected behavior

No error occurs when spreadArray is used on a string containing an emoji/Unicode character.

Current behavior

When spreadArray is used on a string containing an emoji/Unicode character, the following error occurs:

TypeError: Cannot use 'in' operator to search for '0' in ❤️ at __spreadArray

I believe the problem is related to https://github.com/microsoft/tslib/blob/2.3.1/tslib.js#L208 because if you run ...

'0' in '❤️';

// ... or ...

__spreadArray([], "❤️", true);

... the same error occurs.

Steps to reproduce

https://codesandbox.io/s/beautiful-cohen-86fygb?file=/src/index.js:220-251

Context

I need to replace some emojis with images so I am using emoji-regex to find them and get the true length of the emoji so that it can be substituted, they recommend using ...

[...emoji].length

... that under certain tsconfig.json options, will be transpiled to ...

__spreadArray([], emoji, true).length

I have tried other variations, including Array.from() and emoji.length, and only the variation above yields accurate lengths. Fortunately, swallowing the error in a try-catch seems to work so it seems the error is not critical for spreadArray to function for some reason and I do not know why.

Contributor guide

No contributing guide indexed for this repository

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 tslib.js around line 208 and the __spreadArray entry point, then reproduce the reported TypeError using the provided emoji snippet or CodeSandbox. Check the emitted helper under the relevant tsconfig options; done means spreading an emoji-containing string no longer throws and produces the expected length.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
tooling
Issue type
Bug
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.