microsoft / tslib

Runtime library for TypeScript helpers.
BSD Zero Clause License
1.25k stars 126 forks source link

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

Open marcandrews opened 2 years ago

marcandrews commented 2 years ago

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.

jcfranco commented 2 years ago

We ran into this recently in a project that uses color where an innocent linting fix exposed this bug.