Skip to content

Commit 96f03f7

Browse files
committed
Add typings for explicit resource management
This is a follow-up to #23818. When targeting any existing ES version, TypeScript transpiles `using ...` and doesn't require any typings. However, when targeting ESNext, it expects `using ...` to be a native feature, and then looks up `[Symbol.dispose]` in the type and fails with a type validation error if it doesn't exist. Since there's no way to do conditional compilation based on target version in TS, I'm adding this property to type declarations but marking with `ts-ignore` so that `Symbol.dispose` not existing is not an error in versions below ESNext.
1 parent 5cdadd6 commit 96f03f7

6 files changed

+12
-0
lines changed

src/lib/libembind_gen.js

+2
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,8 @@ var LibraryEmbind = {
418418
' delete(): void;\n',
419419
' deleteLater(): this;\n',
420420
' isDeleted(): boolean;\n',
421+
' // @ts-ignore - If targeting lower than ESNext, this symbol might not exist.\n',
422+
' [Symbol.dispose](): void;\n',
421423
' clone(): this;\n',
422424
'}\n',
423425
);

test/other/embind_tsgen.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ export interface ClassHandle {
99
delete(): void;
1010
deleteLater(): this;
1111
isDeleted(): boolean;
12+
// @ts-ignore - If targeting lower than ESNext, this symbol might not exist.
13+
[Symbol.dispose](): void;
1214
clone(): this;
1315
}
1416
export interface Test extends ClassHandle {

test/other/embind_tsgen_ignore_1.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ export interface ClassHandle {
2020
delete(): void;
2121
deleteLater(): this;
2222
isDeleted(): boolean;
23+
// @ts-ignore - If targeting lower than ESNext, this symbol might not exist.
24+
[Symbol.dispose](): void;
2325
clone(): this;
2426
}
2527
export interface Test extends ClassHandle {

test/other/embind_tsgen_ignore_2.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ export interface ClassHandle {
88
delete(): void;
99
deleteLater(): this;
1010
isDeleted(): boolean;
11+
// @ts-ignore - If targeting lower than ESNext, this symbol might not exist.
12+
[Symbol.dispose](): void;
1113
clone(): this;
1214
}
1315
export interface Test extends ClassHandle {

test/other/embind_tsgen_ignore_3.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ export interface ClassHandle {
99
delete(): void;
1010
deleteLater(): this;
1111
isDeleted(): boolean;
12+
// @ts-ignore - If targeting lower than ESNext, this symbol might not exist.
13+
[Symbol.dispose](): void;
1214
clone(): this;
1315
}
1416
export interface Test extends ClassHandle {

test/other/embind_tsgen_module.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ export interface ClassHandle {
99
delete(): void;
1010
deleteLater(): this;
1111
isDeleted(): boolean;
12+
// @ts-ignore - If targeting lower than ESNext, this symbol might not exist.
13+
[Symbol.dispose](): void;
1214
clone(): this;
1315
}
1416
export interface Test extends ClassHandle {

0 commit comments

Comments
 (0)