TypeScript

293 readers
3 users here now

For all things TypeScript

founded 2 years ago
MODERATORS
1
2
2
submitted 1 year ago* (last edited 1 year ago) by KaranGauswami to c/typescript
 
 

Typescript has recently released using keyword feature.

I am trying the following code but not able to compile

import fs from "node:fs";
class TempFile implements Disposable {
  #path: string;
  #handle: number;

  constructor(path: string) {
    this.#path = path;
    this.#handle = fs.openSync(path, "w+");
  }

  // other methods

  [Symbol.dispose]() {
    // Close the file and delete it.
    fs.closeSync(this.#handle);
    fs.unlinkSync(this.#path);
  }
}

It is giving this error.

src/index.ts:2:27 - error TS2304: Cannot find name 'Disposable'.

2 class TempFile implements Disposable {
                            ~~~~~~~~~~

src/index.ts:13:11 - error TS2339: Property 'dispose' does not exist on type 'SymbolConstructor'.

13   [Symbol.dispose]() {
             ~~~~~~~

Any ideas ?

3
4
 
 

It's pretty cool, and pretty powerful what you can do with it. Should other languages adopt this approach? I think named types is better for building abstractions, but I haven't come up with a good explanation yet.

5
 
 
6
5
submitted 2 years ago* (last edited 2 years ago) by MrPoopyButthole to c/typescript