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

Update library to use declare class #55611

Closed
dr-vortex opened this issue Sep 2, 2023 · 3 comments
Closed

Update library to use declare class #55611

dr-vortex opened this issue Sep 2, 2023 · 3 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@dr-vortex
Copy link

dr-vortex commented Sep 2, 2023

Currently, declarations for classes consist of an interface for the instance and a declare var for the static side. This is a convoluted way of approaching classes. Instead, Typescript should use declare class, which clarifies the type (since the class is known to be a class, rather than a var object which has a new method and prototype property) and simplifies the definition (only one declaration).

Sample Code

// old
interface Class {
	method(): void;
	property: number;
}

declare var Class: {
	prototype: Class;
	new(arg: string): Class;
	staticMethod(): void;
}

//new
declare class Class {
	method(): void;
	property: number;
	constructor(arg: string);
	static staticMethod(): void;
}

Documentation Link

No response

@dr-vortex dr-vortex changed the title Update library to use Update library to use declare class Sep 2, 2023
@andrewbranch andrewbranch added the Working as Intended The behavior described is the intended behavior; this is not a bug label Sep 5, 2023
@andrewbranch
Copy link
Member

since the class is known to be a class

Which ones specifically are known to be classes?

image
(text version)
❯ node
Welcome to Node.js v20.3.1.
Type ".help" for more information.
> Number
[Function: Number]
> class Foo {}
undefined
> Foo
[class Foo]
> Buffer
[Function: Buffer] {
  poolSize: 8192,
  from: [Function: from],
  copyBytesFrom: [Function: copyBytesFrom],
  of: [Function: of],
  alloc: [Function: alloc],
  allocUnsafe: [Function: allocUnsafe],
  allocUnsafeSlow: [Function: allocUnsafeSlow],
  isBuffer: [Function: isBuffer],
  compare: [Function: compare],
  isEncoding: [Function: isEncoding],
  concat: [Function: concat],
  byteLength: [Function: byteLength],
  [Symbol(kIsEncodingSymbol)]: [Function: isEncoding]
}

More importantly, this pattern is super important for some weird compatibility hacks implemented in @types/node (DefinitelyTyped/DefinitelyTyped#59905)

@andrewbranch andrewbranch closed this as not planned Won't fix, can't repro, duplicate, stale Sep 5, 2023
@dr-vortex
Copy link
Author

@andrewbranch I was thinking more in the DOM lib, since it doesn't share many types with node.

@andrewbranch
Copy link
Member

It increasingly does. And anyway, they look the same:

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

2 participants