2 elegant ways to enable type checking for standalone browser script #3264

Open
opened 2024-12-03 10:15:42 +00:00 by BrandonStudio · 1 comment

These two ways do not change any code logic. We don't need statements like const XLSX = globalThis.XLSX; or const XLSX = window.XLSX;.

1.

Export as namespace in types/index.d.ts

export as namespace XLSX;

Then refer in script:

/// <reference path="../lib/xlsx/types/index.d.ts" />

2.

Create a specific typing file types.d.ts:

import * as _xlsx from './xlsx/types/index';

declare global {
  const XLSX = _xlsx;
}

Then refer in script:

/// <reference path="../lib/types.d.ts" />
These two ways do not change any code logic. We don't need statements like `const XLSX = globalThis.XLSX;` or `const XLSX = window.XLSX;`. ## 1. Export as namespace in `types/index.d.ts` ```ts export as namespace XLSX; ``` Then refer in script: ```js /// <reference path="../lib/xlsx/types/index.d.ts" /> ``` ## 2. Create a specific typing file `types.d.ts`: ```ts import * as _xlsx from './xlsx/types/index'; declare global { const XLSX = _xlsx; } ``` Then refer in script: ```js /// <reference path="../lib/types.d.ts" /> ```
Owner

Thanks for sharing!

The original approach was based on a discussion from 2024 August -- to see it, you will need to join the SheetJS Discord Server

Your second approach is definitely better and we will be updating the docs to use it.

Unfortunately the reference workaround does not apply to HTML (see https://github.com/microsoft/vscode/issues/114115 ) but that bug has been reported to VSCode.

Thanks for sharing! The original approach was based on [a discussion from 2024 August](https://discord.com/channels/1039771292859109436/1039788802220232744/1274035079764775024) -- to see it, you will need to join the [SheetJS Discord Server](https://discord.gg/sheetjs) Your second approach is definitely better and we will be updating the docs to use it. Unfortunately the `reference` workaround does not apply to HTML (see https://github.com/microsoft/vscode/issues/114115 ) but that bug has been reported to VSCode.
Sign in to join this conversation.
No Milestone
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: sheetjs/sheetjs#3264
No description provided.