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: export DescriptionType enum values #259

Merged
merged 1 commit into from
Jun 22, 2024

Conversation

achingbrain
Copy link
Contributor

@achingbrain achingbrain commented Jun 12, 2024

At compliation time the values of a const enum in TS get changed to numbers which breaks JS APIs that expect the string values.

At runtime anything in a .d.ts file is not available, so the solution is to export enum definitions from the .d.ts file as a regular enum (no const) and export the matching values from the implementation file as a plain object.

@achingbrain
Copy link
Contributor Author

achingbrain commented Jun 12, 2024

That is, I have the following code:

import { DescriptionType } from 'node-datachannel'

peerConnection.setLocalDescription(DescriptionType.offer)

With the current release:

// index.d.ts
export const enum DescriptionType {
...

I get a TS compilation error:

Cannot access ambient const enums when 'verbatimModuleSyntax' is enabled.ts(2748)

With:

// index.d.ts
export enum DescriptionType {
...
// index.js
// no DescriptionType export

TS is happy but JS is not:

import { DescriptionType } from 'node-datachannel'
         ^^^^^^^^^^^^^^^
SyntaxError: The requested module 'node-datachannel' does not provide an export named 'DescriptionType'

With what libdatachannel actually wants:

peerConnection.setLocalDescription('offer')

I get:

Argument of type '"offer"' is not assignable to parameter of type 'DescriptionType | undefined'.ts(2345)

With the changes in this PR TS, JS and C++ are all happy.

At compliation time the values of a `const enum` in TS get changed
to numbers which breaks JS APIs that expect the string values.

At runtime anything in a `.d.ts` file is not available, so the
solution is to export enum definitions from the `.d.ts` file as a
regular `enum` (no `const`) and export the matching values from the
implementation file as a plain object.
@murat-dogan
Copy link
Owner

I am OK with that.
And also we need to add other enums then.

@murat-dogan murat-dogan merged commit 3bd4082 into murat-dogan:master Jun 22, 2024
1 check passed
@achingbrain achingbrain deleted the fix/export-enum-values branch September 12, 2024 09:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants