Improve null type checking of Platform.select (#41455)

This commit is contained in:
Arnaud Barré
2020-01-08 12:01:38 +01:00
committed by Eloy Durán
parent e8560d9532
commit fd9b438118
3 changed files with 14 additions and 7 deletions
+2 -1
View File
@@ -5569,7 +5569,8 @@ interface PlatformStatic {
/**
* @see https://facebook.github.io/react-native/docs/platform-specific-code.html#content
*/
select<T>(specifics: { [platform in PlatformOSType | 'default']?: T }): T;
select<T>(specifics: ({ [platform in PlatformOSType]?: T } & { default: T }) | ({ [platform in PlatformOSType]: T })): T;
select<T>(specifics: { [platform in PlatformOSType]?: T }): T | undefined;
}
interface PlatformIOSStatic extends PlatformStatic {
+11 -5
View File
@@ -21,6 +21,7 @@ import {
CheckBox,
ColorPropType,
DataSourceAssetCallback,
DeviceEventEmitter,
DeviceEventEmitterStatic,
Dimensions,
Image,
@@ -242,7 +243,7 @@ const combinedStyle5: StyleProp<TextStyle> = StyleSheet.compose(
Math.random() < 0.5 ? composeTextStyle : null,
);
const combinedStyle6: StyleProp<TextStyle> = StyleSheet.compose(
const combinedStyle6: StyleProp<TextStyle | null> = StyleSheet.compose(
null,
null,
);
@@ -438,7 +439,7 @@ export class SectionListTest extends React.Component<SectionListProps<string>, {
}
scrollMe = () => {
this.myList.current.scrollToLocation({ itemIndex: 0, sectionIndex: 1 });
this.myList.current && this.myList.current.scrollToLocation({ itemIndex: 0, sectionIndex: 1 });
};
render() {
@@ -661,7 +662,7 @@ const dataSourceAssetCallback1: DataSourceAssetCallback = {
const dataSourceAssetCallback2: DataSourceAssetCallback = {};
// DeviceEventEmitterStatic
const deviceEventEmitterStatic: DeviceEventEmitterStatic = null;
const deviceEventEmitterStatic: DeviceEventEmitterStatic = DeviceEventEmitter;
deviceEventEmitterStatic.addListener("keyboardWillShow", data => true);
deviceEventEmitterStatic.addListener("keyboardWillShow", data => true, {});
@@ -726,7 +727,7 @@ class TextInputTest extends React.Component<{}, { username: string }> {
render() {
return (
<View>
<Text onPress={() => this.username.focus()}>Username</Text>
<Text onPress={() => this.username && this.username.focus()}>Username</Text>
<TextInput
ref={input => (this.username = input)}
@@ -776,7 +777,7 @@ export class ImageTest extends React.Component {
const image: ImageResolvedAssetSource = Image.resolveAssetSource({ uri });
console.log(image.width, image.height, image.scale, image.uri);
Image.queryCache([uri]).then(({ [uri]: status }) => {
Image.queryCache && Image.queryCache([uri]).then(({ [uri]: status }) => {
if (status === undefined) {
console.log("Image is not in cache");
} else {
@@ -985,6 +986,11 @@ const PlatformTest = () => {
}
};
Platform.select({ android: 1 }); // $ExpectType number | undefined
Platform.select({ android: 1, ios: 2, default: 0 }); // $ExpectType number
Platform.select({ android: 1, ios: 2, macos: 3, web: 4, windows: 5 }); // $ExpectType number
Platform.select({ android: 1, ios: 2, macos: 3, web: 4, windows: 5, default: 0 }); // $ExpectType number
// ProgressBarAndroid
const ProgressBarAndroidTest = () => {
<ProgressBarAndroid
+1 -1
View File
@@ -4,7 +4,7 @@
"lib": ["es6"],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": false,
"strictNullChecks": true,
"strictFunctionTypes": true,
"baseUrl": "../",
"jsx": "react",