fix: Fix TypeScript compilation in a Node.js environment#256
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR fixes TypeScript compilation issues when using the Apache Arrow JavaScript library in Node.js environments by removing DOM types dependency and updating type definitions.
- Removes "DOM" from the TypeScript lib configuration to avoid browser-specific type assumptions
- Updates type definitions to use Node.js stream types instead of DOM-specific types
- Replaces
BufferSourcetype with a more generic parameter type forTextDecoder.decode()
Reviewed Changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| tsconfig/tsconfig.base.json | Removes "DOM" from TypeScript lib array to support Node.js environments |
| src/util/utf8.ts | Replaces DOM-specific BufferSource type with generic parameter type |
| src/util/compat.ts | Adds import for Node.js UnderlyingSink type |
| src/util/buffer.ts | Adds import for Node.js ReadableStreamReadResult type |
| src/io/interfaces.ts | Adds import for Node.js StreamPipeOptions type |
| src/io/adapters.ts | Adds import for Node.js ReadableStreamReadValueResult type |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
src/util/utf8.ts
Outdated
| const decoder = new TextDecoder('utf-8'); | ||
| /** @ignore */ | ||
| export const decodeUtf8 = (buffer?: BufferSource) => decoder.decode(buffer); | ||
| export const decodeUtf8 = (buffer?: Parameters<InstanceType<typeof TextDecoder>['decode']>[0]) => decoder.decode(buffer); |
There was a problem hiding this comment.
The type Parameters<InstanceType<typeof TextDecoder>['decode']>[0] is overly complex and hard to read. Consider extracting this to a type alias or using a simpler approach like ArrayBuffer | ArrayBufferView | null | undefined which matches the actual parameter type.
| export const decodeUtf8 = (buffer?: Parameters<InstanceType<typeof TextDecoder>['decode']>[0]) => decoder.decode(buffer); | |
| export const decodeUtf8 = (buffer?: ArrayBuffer | ArrayBufferView | null | undefined) => decoder.decode(buffer); |
src/util/utf8.ts
Outdated
| const decoder = new TextDecoder('utf-8'); | ||
| /** @ignore */ | ||
| export const decodeUtf8 = (buffer?: BufferSource) => decoder.decode(buffer); | ||
| export const decodeUtf8 = (buffer?: Parameters<InstanceType<typeof TextDecoder>['decode']>[0]) => decoder.decode(buffer); |
There was a problem hiding this comment.
I think this is also an option?
| export const decodeUtf8 = (buffer?: Parameters<InstanceType<typeof TextDecoder>['decode']>[0]) => decoder.decode(buffer); | |
| export const decodeUtf8 = decoder.decode.bind(decoder); |
Fixes apache#45 This library runs in modern browsers, but is also intended for use in maintained versions of Node.js, which means that the TypeScript types cannot assume `"lib": ["DOM"]`. This change removes this `lib`, and updates the types to compile. Given that this library runs in the browser, it feels strange to import types from `node`, but this is no worse than the current situation, which already contains imports from `node`.
03c9554 to
5adda16
Compare
Fixes #45
This library runs in modern browsers, but is also intended for use in maintained versions of Node.js, which means that the TypeScript types cannot assume
"lib": ["DOM"].This change removes this
lib, and updates the types to compile.Given that this library runs in the browser, it feels strange to import types from
node, but this is no worse than the current situation, which already contains imports fromnode.