-
Notifications
You must be signed in to change notification settings - Fork 12.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
react-native typings not work since typescript 3.1.1 #27421
Comments
I don't understand what's going on in the type inference on the call to The rest I do understand: after the |
Reduced example (same behavior in 3.0.3 and 3.2.0-dev.20180927): interface TextStyle extends ViewStyle {
fontWeight?: "900";
}
interface ViewStyle {
marginTop?: number;
}
declare function create<T extends { [P in keyof T]: TextStyle | ViewStyle }>(styles: T): T;
const s = create({
didNotWork: {
fontWeight: '900', // if we comment this line, errors gone
marginTop: 5, // if this line commented, errors also gone
},
});
const f1: TextStyle = s.didNotWork; |
This is working as intended, but it's subtle to say the least. I'm going to describe what happens using the reduced example posted by @mattmccutchen above. First, because the constraint for We then check for assignability to the instantiated constraint, i.e. If you comment out As I said, it's subtle! I'd suggest changing the constraint for declare function create<T extends { [x: string]: TextStyle | ViewStyle }>(styles: T): T; With that change it works as expected because everything gets properly contextually typed. |
@ahejlsberg I modified reduced example to check this: interface TextStyle extends ViewStyle { fontWeight?: "900"; }
interface ViewStyle { marginTop?: number; }
declare function create<T extends { [P: string]: TextStyle | ViewStyle }>(styles: T): T
const s = create({
didNotWork: {
fontWeight: '900', // if we comment this line, errors gone
marginTop: 5, // if this line commented, errors also gone
},
})
const f1: TextStyle = s.didNotWork
const f2: ViewStyle = s.notExists
// ^--- Property 'notExists' does not exist on type '{ didNotWork: { fontWeight: "900"; marginTop: number; }; }'. It is good that last line emit error in context of recommended fix. But isn't it counter intuitive? I don't understand this. |
If the behaviour is as expected, is it reasonable to assume that the React Native typings need to be updated to eliminate this discrepancy? Until this is fixed I guess the workaround is to use TS 3.0.3 if you want to use |
Here |
@mattmccutchen Thank you for explanation! Then this definitely can be fixed on typings side :-) |
Can I close this issue now? It seems no work can be done on this side... |
Yes please. Otherwise, the TypeScript team has an automated process that will eventually close it based on the "Working as Intended" label. |
@vovkasm So what now ? |
@ScreamZ I don't know... most probably we will not be able express such types reliable until typescript 10 or 20 :-/ |
@vovkasm GeekyAnts/NativeBase#2123 Looks like the same with native-base |
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 export function create<T extends NamedStyles<T> | NamedStyles<any>>(styles: T): { [P in keyof T]: RegisteredStyle<T[P]> }; The Best I can tell this fixes the issue and doesn't require any other changes. |
@ahejlsberg Could you provide an example of how exactly do you override |
Fixes #29265, based on comment microsoft/TypeScript#27421 (comment)
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:
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. |
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.git
cd rn-ts-3.1.1
npm install && npm test
Paste here for easy reading
Expected behavior:
No errors
Actual behavior:
An error occured:
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:
The text was updated successfully, but these errors were encountered: