Have you ever read official guide of using Typescript in React Native. Did you mention something strange in tsconfig.json
?
"lib": ["es2017"]
lib option looks weird, 5 year old standard recommended as default, isn't it?
If you wants to use some modern methods:
[].flat(...)
[].flatMap(...)
Object.fromEntries({ })
-
'str'.trimEnd()
...
Typescript will politely throw an error:
Object.fromEntries(...)
// ^^^^^^^^^^ TS2550: Property 'fromEntries' does not exist on type 'ObjectConstructor'.
// Do you need to change your target library?
// Try changing the 'lib' compiler option to 'es2019' or later.
But, wait when I run this code on simulator iOS & Android I didn't get any runtime errors.
So, is it mean that documentation is outdated?
Actually no, everything depends on JavaScript engine that will run your code
By default React Native use:
- JSC (JavaScriptCore) that build in iOS
jsc-android
Note: Also, include some polyfills for many standard functions
P.S. For me, that was a surprised that
Promise
is polyfilled too :(
For example, if you try to use Array.prototype.flat()
your app will crash for users who using iOS 11
. Yes this one reason why es2017
And iOS 11 is a minimal supported version by React Native (till 0.68.x
)
but starting from React Native
0.69.0
iOS12.4
is minimal supported version (thanks Meta!)
Great thing that you haven't ever need to looking for information about JavaScript modern features and Engines that support them.
Now it is easier to compare Typescript lib options \ JS features and JS Engines
Anyway, Meta also developing their own Javascript Engine Hermes that tried to align all new EcmaScript specifications
Even more, you can use V8 JavaScript Engine
react-native-v8
has the greatest support for all modern JS features.
You can comprate all JavaScript Engines compatibility table and verify you tsconfig, if if it use a right lib option!
Useful links links:
- React Native Typescript Config
- ECMAScript 6 compatibility table for React Native engines
react-native-v8
- Hermes engine
jsc-android
- Commit where minimal iOS version was bumped
Top comments (0)