Skip to content
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

fix types #330

Merged
merged 1 commit into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/expo-metro-runtime/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

### 🐛 Bug fixes

- Fix types.

### 💡 Others

## [Mon, 27 Feb 2023 17:48:01 -0600](https://github.com/expo/router/commit/3b757523236e2f2f23d7c5b874155c806313eadc)
Expand Down
46 changes: 27 additions & 19 deletions packages/expo-metro-runtime/src/HMRClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ import getDevServer from "./getDevServer";
const MetroHMRClient = require("metro-runtime/src/modules/HMRClient");
const pendingEntryPoints: string[] = [];

let hmrClient: {
type HMRClientType = {
send: (msg: string) => void;
isEnabled: () => boolean;
disable: () => void;
enable: () => void;
hasPendingUpdates: () => boolean;
} | null = null;
};

let hmrClient: HMRClientType | null = null;
let hmrUnavailableReason: string | null = null;
let currentCompileErrorMessage: string | null = null;
let didConnect: boolean = false;
Expand Down Expand Up @@ -157,7 +159,7 @@ const HMRClient: HMRClientNativeInterface = {
fullBundleUrl
);

client.on("connection-error", (e) => {
client.on("connection-error", (e: Error) => {
let error = `Cannot connect to Metro.

Try the following to fix the issue:
Expand All @@ -171,27 +173,33 @@ const HMRClient: HMRClientNativeInterface = {
setHMRUnavailableReason(error);
});

client.on("update-start", ({ isInitialUpdate }) => {
currentCompileErrorMessage = null;
didConnect = true;
client.on(
"update-start",
({ isInitialUpdate }: { isInitialUpdate?: boolean }) => {
currentCompileErrorMessage = null;
didConnect = true;

if (client.isEnabled() && !isInitialUpdate) {
LoadingView.showMessage("Refreshing...", "refresh");
if (client.isEnabled() && !isInitialUpdate) {
LoadingView.showMessage("Refreshing...", "refresh");
}
}
});
);

client.on("update", ({ isInitialUpdate }) => {
if (client.isEnabled() && !isInitialUpdate) {
dismissRedbox();
LogBox.clearAllLogs();
client.on(
"update",
({ isInitialUpdate }: { isInitialUpdate?: boolean }) => {
if (client.isEnabled() && !isInitialUpdate) {
dismissRedbox();
LogBox.clearAllLogs();
}
}
});
);

client.on("update-done", () => {
LoadingView.hide();
});

client.on("error", (data) => {
client.on("error", (data: { type: string; message: string }) => {
LoadingView.hide();

if (data.type === "GraphNotFoundError") {
Expand All @@ -212,7 +220,7 @@ const HMRClient: HMRClientNativeInterface = {
}
});

client.on("close", (closeEvent) => {
client.on("close", (closeEvent: { code: number; reason: string }) => {
LoadingView.hide();

// https://www.rfc-editor.org/rfc/rfc6455.html#section-7.4.1
Expand Down Expand Up @@ -248,7 +256,7 @@ To reconnect:
},
};

function setHMRUnavailableReason(reason) {
function setHMRUnavailableReason(reason: string) {
assert(hmrClient, "Expected HMRClient.setup() call at startup.");
if (hmrUnavailableReason !== null) {
// Don't show more than one warning.
Expand All @@ -265,15 +273,15 @@ function setHMRUnavailableReason(reason) {
}
}

function registerBundleEntryPoints(client) {
function registerBundleEntryPoints(client: HMRClientType | null) {
if (hmrUnavailableReason != null) {
// "Bundle Splitting – Metro disconnected"
window.location.reload();
return;
}

if (pendingEntryPoints.length > 0) {
client.send(
client?.send(
JSON.stringify({
type: "register-entrypoints",
entryPoints: pendingEntryPoints,
Expand Down
22 changes: 14 additions & 8 deletions packages/expo-metro-runtime/src/async-require/fetchAsync.native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,40 @@ import { Platform } from "react-native";
// @ts-expect-error
import Networking from "react-native/Libraries/Network/RCTNetworking";

type Subscriber = { remove: () => void };

export function fetchAsync(
url: string
): Promise<{ body: string; headers: Record<string, string> }> {
let id: string | null = null;
let responseText: string | null = null;
let headers: Record<string, string> = {};
let dataListener: { remove: () => void } | null = null;
let completeListener: { remove: () => void } | null = null;
let responseListener: { remove: () => void } | null = null;
let dataListener: Subscriber | null = null;
let completeListener: Subscriber | null = null;
let responseListener: Subscriber | null = null;
return new Promise<{ body: string; headers: Record<string, string> }>(
(resolve, reject) => {
dataListener = Networking.addListener(
const addListener = Networking.addListener as (
event: string,
callback: (props: [string, any, any]) => any
) => Subscriber;
dataListener = addListener(
"didReceiveNetworkData",
([requestId, response]) => {
if (requestId === id) {
responseText = response;
}
}
);
responseListener = Networking.addListener(
responseListener = addListener(
"didReceiveNetworkResponse",
([requestId, status, responseHeaders]) => {
if (requestId === id) {
headers = responseHeaders;
}
}
);
completeListener = Networking.addListener(
completeListener = addListener(
"didCompleteNetworkResponse",
([requestId, error]) => {
if (requestId === id) {
Expand All @@ -49,7 +55,7 @@ export function fetchAsync(
}
}
);
Networking.sendRequest(
(Networking.sendRequest as any)(
"GET",
"asyncRequest",
url,
Expand All @@ -60,7 +66,7 @@ export function fetchAsync(
"text",
false,
0,
(requestId) => {
(requestId: string) => {
id = requestId;
},
true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ declare let global: {
export function fetchThenEvalAsync(url: string): Promise<void> {
return fetchAsync(url).then(({ body, headers }) => {
if (
headers["Content-Type"] != null &&
headers["Content-Type"].indexOf("application/json") >= 0
headers.has("Content-Type") != null &&
headers.get("Content-Type")!.includes("application/json")
) {
// Errors are returned as JSON.
throw new Error(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
// @ts-expect-error
import NativeDevSplitBundleLoader from "react-native/Libraries/Utilities/NativeDevSplitBundleLoader";

import { loadBundleAsync as loadBundlePolyfillAsync } from "./loadBundlePolyfill";
Expand All @@ -13,11 +14,13 @@ export function loadBundleAsync(bundlePath: string): Promise<any> {
// NOTE(EvanBacon): This is broken on iOS afaict
NativeDevSplitBundleLoader?.loadBundle
) {
return NativeDevSplitBundleLoader.loadBundle(bundlePath).catch((e) => {
// On Android 'e' is not an instance of Error, which seems to be a bug.
// As a workaround, re-throw an Error to not break the error handling code.
throw new Error(e.message);
});
return NativeDevSplitBundleLoader.loadBundle(bundlePath).catch(
(e: Error) => {
// On Android 'e' is not an instance of Error, which seems to be a bug.
// As a workaround, re-throw an Error to not break the error handling code.
throw new Error(e.message);
}
);
}
return loadBundlePolyfillAsync(bundlePath);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* LICENSE file in the root directory of this source tree.
*/

// @ts-expect-error
import UTFSequence from "react-native/Libraries/UTFSequence";

import type { LogBoxLogData } from "./LogBoxLog";
Expand Down
14 changes: 7 additions & 7 deletions packages/expo-metro-runtime/src/error-overlay/LogBox.web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ if (__DEV__) {
parseInterpolation,
} = require("./Data/parseLogBoxLog");

let originalConsoleError;
let consoleErrorImpl;
let originalConsoleError: typeof console.error | undefined;
let consoleErrorImpl: typeof console.error | undefined;

let isLogBoxInstalled: boolean = false;

Expand All @@ -60,7 +60,7 @@ if (__DEV__) {
originalConsoleError = console.error.bind(console);

console.error = (...args) => {
consoleErrorImpl(...args);
consoleErrorImpl?.(...args);
};
}

Expand Down Expand Up @@ -119,10 +119,10 @@ if (__DEV__) {
return typeof args[0] === "string" && args[0].startsWith("Warning: ");
};

const registerError = (...args): void => {
const registerError = (...args: Parameters<typeof console.error>): void => {
// Let errors within LogBox itself fall through.
if (LogBoxData.isLogBoxErrorMessage(args[0])) {
originalConsoleError(...args);
originalConsoleError?.(...args);
return;
}

Expand All @@ -135,7 +135,7 @@ if (__DEV__) {
//
// The 'warning' module needs to be handled here because React internally calls
// `console.error('Warning: ')` with the component stack already included.
originalConsoleError(...args);
originalConsoleError?.(...args);
return;
}

Expand All @@ -145,7 +145,7 @@ if (__DEV__) {
// Interpolate the message so they are formatted for adb and other CLIs.
// This is different than the message.content above because it includes component stacks.
const interpolated = parseInterpolation(args);
originalConsoleError(interpolated.message.content);
originalConsoleError?.(interpolated.message.content);
}
} catch (err) {
LogBoxData.reportUnexpectedLogBoxError(err);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import React from "react";
import { StyleProp, StyleSheet, Text, TextStyle, View } from "react-native";

// Afterglow theme from https://iterm2colorschemes.com/
const COLORS = {
const COLORS: Record<string, string> = {
"ansi-black": "rgb(27, 27, 27)",
"ansi-red": "rgb(187, 86, 83)",
"ansi-green": "rgb(144, 157, 98)",
Expand Down Expand Up @@ -57,7 +57,7 @@ export function Ansi({
}
});

const getText = (content, key) => {
const getText = (content: string, key: number) => {
if (key === 1) {
// Remove the vertical bar after line numbers
return content.replace(/\| $/, " ");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-expect-error
import NativeLogBox from "react-native/Libraries/NativeModules/specs/NativeLogBox";

export default NativeLogBox;
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-expect-error
import openFileInEditor from "react-native/Libraries/Core/Devtools/openFileInEditor";

export default openFileInEditor;
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import * as LogBoxStyle from "../UI/LogBoxStyle";

type Props = {
heading: string;
children;
children: React.ReactNode;
action?: any;
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
export default function ErrorToastContainer({ children }) {
return children;
import React from "react";

export default function ErrorToastContainer({
children,
}: {
children: React.ReactNode;
}): React.ReactElement {
return <>{children}</>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from "react";

import ExceptionsManager from "./modules/ExceptionsManager";

function useStackTraceLimit(limit) {
function useStackTraceLimit(limit: number) {
const current = React.useRef(0);
React.useEffect(() => {
try {
Expand Down
1 change: 1 addition & 0 deletions packages/expo-metro-runtime/src/setupFastRefresh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ const Refresh = {

// The metro require polyfill can not have dependencies (applies for all polyfills).
// Expose `Refresh` by assigning it to global to make it available in the polyfill.
// @ts-expect-error
global[(global.__METRO_GLOBAL_PREFIX__ || "") + "__ReactRefresh"] = Refresh;
3 changes: 2 additions & 1 deletion packages/expo-metro-runtime/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"outDir": "./build",
"target": "es2019",
"module": "commonjs",
"types": ["node", "jest"]
"types": ["node", "jest"],
"strict": true
},
"include": ["./src"],
"exclude": ["**/__mocks__/*", "**/__tests__/*"]
Expand Down
2 changes: 2 additions & 0 deletions packages/expo-router/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

### 🐛 Bug fixes

- Fix types.

### 💡 Others

## [Mon, 27 Feb 2023 17:48:01 -0600](https://github.com/expo/router/commit/3b757523236e2f2f23d7c5b874155c806313eadc)
Expand Down
1 change: 1 addition & 0 deletions packages/expo-router/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
},
"devDependencies": {
"@react-navigation/drawer": "^6.6.2",
"@types/url-parse": "^1.4.8",
"expo-splash-screen": "~0.18.1",
"expo-status-bar": "~1.4.4",
"react-native-gesture-handler": "~2.9.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/expo-router/src/LocationProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ export function getNormalizedStatePath({
prev[key] = decodeURIComponent(value as string);
}
return prev;
}, {}),
}, {} as SearchParams),
};
}

Expand Down
2 changes: 1 addition & 1 deletion packages/expo-router/src/fork/getPathFromState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ function getParamsWithConventionsCollapsed({
routeName: string;
params: object;
}): Record<string, string> {
const processedParams = { ...params };
const processedParams: Record<string, string> = { ...params };

// Remove the params present in the pattern since we'll only use the rest for query string

Expand Down
1 change: 1 addition & 0 deletions packages/expo-router/src/fork/getStateFromPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,7 @@ const createNormalizedConfigs = (

parentScreens.push(screen);

// @ts-expect-error
const config = routeConfig[screen];

if (typeof config === "string") {
Expand Down
Loading