Skip to content

Commit

Permalink
Fix an annoying TypeScript error
Browse files Browse the repository at this point in the history
Apparently, TypeScript can't deal with default values for parameters
with inferred types:
microsoft/TypeScript#29049
  • Loading branch information
noahbrenner committed Jan 9, 2021
1 parent d39f4c1 commit 70df618
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
6 changes: 3 additions & 3 deletions src/pages/instrument.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import React, { Suspense, useEffect, useState } from "react";
import { getInstrumentById } from "#api";
import NotFound from "#src/pages/404";
import type { IInstrument } from "#src/types";
import { lazy } from "#src/utils";
import { lazyNamed } from "#src/utils";

const Instrument = lazy(() => import("#layouts/Instrument"), "Instrument");
const InstrumentForm = lazy(
const Instrument = lazyNamed(() => import("#layouts/Instrument"), "Instrument");
const InstrumentForm = lazyNamed(
() => import("#layouts/InstrumentForm"),
"InstrumentForm"
);
Expand Down
13 changes: 6 additions & 7 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import { lazy as reactLazy } from "react";
import { lazy } from "react";
import type { ComponentType, LazyExoticComponent } from "react";

/**
* Lazily import a named or default exported component
* Lazily import a named exported component
*
* const Named = lazy(() => import("./Named"), "Named");
* const Default = lazy(() => import("./Default")); // Omit 2nd argument
* const Component = lazyNamed(() => import("./Component"), "Component");
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function lazy<T extends ComponentType<any>, K extends string>(
export function lazyNamed<T extends ComponentType<any>, K extends string>(
factory: () => Promise<{ [key in K]: T }>,
exportedName: K = "default"
exportedName: K
): LazyExoticComponent<T> {
return reactLazy(async () => ({
return lazy(async () => ({
// NOTE This dynamic property access breaks tree shaking for the imported
// module! That isn't impacting this site's bundles though, since we only
// export one component per module.
Expand Down

0 comments on commit 70df618

Please sign in to comment.