Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(core): a swizzled copy type-checks without Node's types (#10252)

Cindy Zhang66b09526b959
diff --git a/.changeset/core-process-env-types.md b/.changeset/core-process-env-types.md
new file mode 100644
index 0000000000..6d51397c33
--- /dev/null
+++ b/.changeset/core-process-env-types.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] The Core modules that read `process.env.NODE_ENV` for development warnings now declare it themselves, so those files type-check in a project that does not install `@types/node`, such as a swizzled `Breadcrumbs`, `Stepper` or `TabList`. Bundlers still replace the value at build time, so production builds drop the warnings as before. No API changes.
+
+@cixzhang
diff --git a/packages/core/src/Breadcrumbs/BreadcrumbItem.tsx b/packages/core/src/Breadcrumbs/BreadcrumbItem.tsx
index 222f44ddf6..098835876f 100644
--- a/packages/core/src/Breadcrumbs/BreadcrumbItem.tsx
+++ b/packages/core/src/Breadcrumbs/BreadcrumbItem.tsx
@@ -80,6 +80,10 @@ import {
} from '../AppShell/useCloseMobileDrawerOnActivate';
import {useInteractionModalityTracking} from '../utils/interactionModality';
import {isRenderable} from '../utils/isRenderable';
+
+// Bundlers replace this value at build time. The declaration lets a product
+// without Node's types type-check this file.
+declare const process: {readonly env: {readonly NODE_ENV?: string}};
// =============================================================================
// Props
// =============================================================================
diff --git a/packages/core/src/Markdown/plugins/protocol.ts b/packages/core/src/Markdown/plugins/protocol.ts
index 00c93a56cc..7a6a61fa90 100644
--- a/packages/core/src/Markdown/plugins/protocol.ts
+++ b/packages/core/src/Markdown/plugins/protocol.ts
@@ -18,6 +18,10 @@ import type {
MarkdownAstRoot,
} from '../ast';
+// Bundlers replace this value at build time. The declaration lets a product
+// without Node's types type-check this file.
+declare const process: {readonly env: {readonly NODE_ENV?: string}};
+
export type MarkdownPluginData = MarkdownAstDataValue;
export type MarkdownExtensionNode<
diff --git a/packages/core/src/MultiSelector/MultiSelector.tsx b/packages/core/src/MultiSelector/MultiSelector.tsx
index cbe0ad2001..3bf93f4768 100644
--- a/packages/core/src/MultiSelector/MultiSelector.tsx
+++ b/packages/core/src/MultiSelector/MultiSelector.tsx
@@ -116,6 +116,10 @@ import {
} from '../i18n/useComponentTranslator';
import english from '../i18n/generated-english/MultiSelector/MultiSelector.generated';
+// Bundlers replace this value at build time. The declaration lets a product
+// without Node's types type-check this file.
+declare const process: {readonly env: {readonly NODE_ENV?: string}};
+
registerEnglishFallback(english);
// Sentinel value for the select-all item in keyboard navigation
diff --git a/packages/core/src/Stepper/Stepper.tsx b/packages/core/src/Stepper/Stepper.tsx
index f60d2c794b..c0e67e5a68 100644
--- a/packages/core/src/Stepper/Stepper.tsx
+++ b/packages/core/src/Stepper/Stepper.tsx
@@ -49,6 +49,10 @@ import {
} from '../i18n/useComponentTranslator';
import english from '../i18n/generated-english/Stepper/Stepper.generated';
+// Bundlers replace this value at build time. The declaration lets a product
+// without Node's types type-check this file.
+declare const process: {readonly env: {readonly NODE_ENV?: string}};
+
registerEnglishFallback(english);
/**
diff --git a/packages/core/src/TabList/TabList.tsx b/packages/core/src/TabList/TabList.tsx
index c15b2279fe..b3c3ea2018 100644
--- a/packages/core/src/TabList/TabList.tsx
+++ b/packages/core/src/TabList/TabList.tsx
@@ -64,6 +64,10 @@ import {
} from '../i18n/useComponentTranslator';
import english from '../i18n/generated-english/TabList/TabList.generated';
+// Bundlers replace this value at build time. The declaration lets a product
+// without Node's types type-check this file.
+declare const process: {readonly env: {readonly NODE_ENV?: string}};
+
registerEnglishFallback(english);
/**
* Selector matching the focusable stops in the tab strip: every Tab
diff --git a/packages/core/src/Table/plugins/rowStatus/useTableRowStatus.tsx b/packages/core/src/Table/plugins/rowStatus/useTableRowStatus.tsx
index 14c3e76fd4..8da50e283b 100644
--- a/packages/core/src/Table/plugins/rowStatus/useTableRowStatus.tsx
+++ b/packages/core/src/Table/plugins/rowStatus/useTableRowStatus.tsx
@@ -30,6 +30,10 @@ import {
} from '../../../i18n/useComponentTranslator';
import english from '../../../i18n/generated-english/Table/plugins/rowStatus/useTableRowStatus.generated';
+// Bundlers replace this value at build time. The declaration lets a product
+// without Node's types type-check this file.
+declare const process: {readonly env: {readonly NODE_ENV?: string}};
+
registerEnglishFallback(english);
/**
diff --git a/packages/core/src/utils/devWarning.ts b/packages/core/src/utils/devWarning.ts
index cd16aa3503..ad8f4960d0 100644
--- a/packages/core/src/utils/devWarning.ts
+++ b/packages/core/src/utils/devWarning.ts
@@ -16,6 +16,10 @@
* - /packages/core/src/hooks/useDevWarning.ts (the render-path counterpart)
*/
+// Bundlers replace this value at build time. The declaration lets a product
+// without Node's types type-check this file.
+declare const process: {readonly env: {readonly NODE_ENV?: string}};
+
const isDev = process.env.NODE_ENV !== 'production';
/** Format a message as `Component: message`. */
diff --git a/packages/core/src/utils/processEnvTypes.test.ts b/packages/core/src/utils/processEnvTypes.test.ts
new file mode 100644
index 0000000000..7ec3b5be98
--- /dev/null
+++ b/packages/core/src/utils/processEnvTypes.test.ts
@@ -0,0 +1,66 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+/**
+ * A swizzled copy is built by a product that may not install Node's types.
+ * Bundlers replace `process.env.NODE_ENV` at build time, so a Core module that
+ * reads it must still type-check without those types.
+ */
+
+import * as fs from 'node:fs';
+import * as path from 'node:path';
+import {fileURLToPath} from 'node:url';
+import ts from 'typescript';
+import {describe, expect, it} from 'vitest';
+
+const SRC = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
+
+/** Core's source modules, without tests, stories and declaration files. */
+function sourceModules(): string[] {
+ return fs
+ .readdirSync(SRC, {recursive: true, withFileTypes: true})
+ .filter(
+ entry =>
+ entry.isFile() &&
+ /\.tsx?$/.test(entry.name) &&
+ !/\.(test|spec|stories|d)\.tsx?$/.test(entry.name) &&
+ !entry.parentPath.includes('__tests__'),
+ )
+ .map(entry => path.join(entry.parentPath, entry.name));
+}
+
+/** A module's code without its comments. */
+function codeOf(file: string): string {
+ return fs
+ .readFileSync(file, 'utf-8')
+ .replace(/\/\*[\s\S]*?\*\//g, '')
+ .replace(/^\s*\/\/.*$/gm, '');
+}
+
+describe('process.env without Node types', () => {
+ it('every module that reads process.env type-checks without them', () => {
+ const readers = sourceModules().filter(file =>
+ /\bprocess\.env\b/.test(codeOf(file)),
+ );
+ expect(readers.length).toBeGreaterThan(0);
+ const program = ts.createProgram(readers, {
+ target: ts.ScriptTarget.ES2022,
+ module: ts.ModuleKind.ESNext,
+ moduleResolution: ts.ModuleResolutionKind.Bundler,
+ jsx: ts.JsxEmit.ReactJSX,
+ lib: ['lib.es2022.d.ts', 'lib.dom.d.ts'],
+ types: [],
+ noResolve: true,
+ noEmit: true,
+ skipLibCheck: true,
+ });
+ // 2580 and 2591: "Cannot find name 'process'", with or without the hint
+ // to install Node's types.
+ const missing = readers.flatMap(file =>
+ program
+ .getSemanticDiagnostics(program.getSourceFile(file))
+ .filter(d => d.code === 2580 || d.code === 2591)
+ .map(d => `${path.relative(SRC, file)}: TS${d.code}`),
+ );
+ expect(missing).toEqual([]);
+ });
+});