TypeScript: react-native typings not work since typescript 3.1.1
TypeScript Version: 3.2.0-dev.20180927
Search Terms: react-native, stylesheet
Code I’m created repo to reproduce:
git clone https://github.com/vovkasm/rn-ts-3.1.1.gitcd rn-ts-3.1.1npm install && npm test
Paste here for easy reading
import React from 'react'
import { StyleSheet, Text } from 'react-native'
const s = StyleSheet.create({
didNotWork: {
fontSize: 16,
fontWeight: '900', // if we comment this line, errors gone
marginTop: 5, // if this line commented, errors also gone
},
work: {
fontSize: 18,
// fontWeight: '900', // when uncommented also work
},
})
export const sample1 = <Text style={s.work} />
export const sample2 = <Text style={s.didNotWork} />
// ^ this line generate error:
// index.tsx:17:30 - error TS2322: Type 'RegisteredStyle<{ fontSize: number; fontWeight: string; marginTop: number; }>' is not assignable to type 'StyleProp<TextStyle>'.
// Type 'RegisteredStyle<{ fontSize: number; fontWeight: string; marginTop: number; }>' is not assignable to type 'RecursiveArray<false | TextStyle | RegisteredStyle<TextStyle> | null | undefined>'.
// Property 'length' is missing in type 'Number & { __registeredStyleBrand: { fontSize: number; fontWeight: string; marginTop: number; }; }'.
// 17 export const sample2 = <Text style={s.didNotWork} />
// ~~~~~
// node_modules/@types/react-native/index.d.ts:907:5
// 907 style?: StyleProp<TextStyle>;
// ~~~~~
// The expected type comes from property 'style' which is declared here on type 'IntrinsicAttributes & IntrinsicClassAttributes<Text> & Readonly<{ children?: ReactNode; }> & Readonly<TextProps>'
export const sample3 = <Text style={{fontSize: 16, fontWeight: '900', marginTop: 5}} />
Expected behavior: No errors
Actual behavior: An error occured:
index.tsx:17:30 - error TS2322: Type 'RegisteredStyle<{ fontSize: number; fontWeight: string; marginTop: number; }>' is not assignable to type 'StyleProp<TextStyle>'.
Type 'RegisteredStyle<{ fontSize: number; fontWeight: string; marginTop: number; }>' is not assignable to type 'RecursiveArray<false | TextStyle | RegisteredStyle<TextStyle> | null | undefined>'.
Property 'length' is missing in type 'Number & { __registeredStyleBrand: { fontSize: number; fontWeight: string; marginTop: number; }; }'.
17 export const sample2 = <Text style={s.didNotWork} />
~~~~~
node_modules/@types/react-native/index.d.ts:907:5
907 style?: StyleProp<TextStyle>;
~~~~~
The expected type comes from property 'style' which is declared here on type 'IntrinsicAttributes & IntrinsicClassAttributes<Text> & Readonly<{ children?: ReactNode; }> & Readonly<TextProps>'
Playground Link: https://github.com/vovkasm/rn-ts-3.1.1
Related Issues: no
Sorry, I can’t found a way to reduce this regression farther to smallest possible example. But it looks like this actually typescript regression because:
- It works with ts 3.0.3
- It magically (from my point of view) will pass test if we change code in unrelated parts (comment/uncomment some styles - see comments in index.tsx)
- I traced type definitions of react-native.d.ts and they appears to be correct (again, from my point of view)…
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 4
- Comments: 15 (6 by maintainers)
In the same area of React Native I found a similar error, after the fix was applied and I think it can be a TypeScript error. I’ve extracted the relevant parts so the repro with the latest TSC is easy.
Code:
After calling TSC test.ts, this is the error:
overflowdefined in 2 different interfaces and I think that TS compiler fails early on mismatching valuesets because the interface types are not related, but one of them could be the result of thecreatemethod call.That is just the top of the cake that in React Native’s case, FlexStyle has the overflow property, so an the ImageStyle interface which extends FlexStyle “overwrites” the valueset, but this time I separated it for clarity.
Reduced example (same behavior in 3.0.3 and 3.2.0-dev.20180927):
I see the fix to react-native got stalled with the fix I suggested (because it adds a new requirement for arguments to have a string index signature). Let me suggest a different fix. Change react-native’s
StyleSheet.createfunction inindex.d.tsto have the following declaration:The
NamedStyles<any>part of the union type causes the constraint to have a string index signature (such that contextual types are provided), whereas theNamedStyles<T>part of the constraint ensures that a final inferred type without a string index signature is accepted.Best I can tell this fixes the issue and doesn’t require any other changes.
@ScreamZ I don’t know… most probably we will not be able express such types reliable until typescript 10 or 20 😕 Changes suggested by typescript team simple did not work…