admin管理员组文章数量:1329105
I have a home-brew JavaScript library (one js file) that contains a number of classes. In order to use it in my TypeScript projects, I have created a d.ts file that contains type definitions for all those classes. A typical definition would look like this:
export interface ClassExample {
new(param: boolean): ClassExample;
propertyName: number|string;
log(...args: any[]): void;
}
And it works well. But I am getting bashed by linter rule @typescript-eslint/no-misused-new
: "Interfaces cannot be constructed, only classes". Of course, I can configure my linter and/or add exceptions for the particular d.ts
file, but this makes me contemplate if there should be some other approach to describe my classes. Should an abstract class
declaration be used instead? I tried to look up how the class constructors are declared for publicly available Node.js modules, and found this code in @types/node/console.d.ts
file:
interface ConsoleConstructor {
prototype: Console;
new(stdout: WritableStream, stderr?: WritableStream, ignoreErrors?: boolean): Console;
new(options: ConsoleConstructorOptions): Console;
}
So, I figure, declaring class constructors in interface is not a grave sin, if the authors of Node.js are practicing it?
I have a home-brew JavaScript library (one js file) that contains a number of classes. In order to use it in my TypeScript projects, I have created a d.ts file that contains type definitions for all those classes. A typical definition would look like this:
export interface ClassExample {
new(param: boolean): ClassExample;
propertyName: number|string;
log(...args: any[]): void;
}
And it works well. But I am getting bashed by linter rule @typescript-eslint/no-misused-new
: "Interfaces cannot be constructed, only classes". Of course, I can configure my linter and/or add exceptions for the particular d.ts
file, but this makes me contemplate if there should be some other approach to describe my classes. Should an abstract class
declaration be used instead? I tried to look up how the class constructors are declared for publicly available Node.js modules, and found this code in @types/node/console.d.ts
file:
interface ConsoleConstructor {
prototype: Console;
new(stdout: WritableStream, stderr?: WritableStream, ignoreErrors?: boolean): Console;
new(options: ConsoleConstructorOptions): Console;
}
So, I figure, declaring class constructors in interface is not a grave sin, if the authors of Node.js are practicing it?
Share Improve this question asked Feb 10, 2021 at 11:08 PassidayPassiday 8,10511 gold badges45 silver badges65 bronze badges1 Answer
Reset to default 8According to the handbook, you'd declare that class with the declare
keyword, like this:
declare class ClassExample {
constructor(param: boolean);
propertyName: number|string;
log(...args: any[]): void;
}
Here's the section linked above as it appears as of when this answer was written (my emphasis):
Classes
Documentation
You can create a
greeter
by instantiating theGreeter
object, or create a customizedgreeter
by extending from it.Code
const myGreeter = new Greeter("hello, world"); myGreeter.greeting = "howdy"; myGreeter.showGreeting(); class SpecialGreeter extends Greeter { constructor() { super("Very special greetings"); } }
Declaration
Use
declare class
to describe aclass
orclass
-like object. Classes can have properties and methods as well as aconstructor
.declare class Greeter { constructor(greeting: string); greeting: string; showGreeting(): void; }
本文标签: javascriptTypeScript type definitions how to declare class constructorsStack Overflow
版权声明:本文标题:javascript - TypeScript type definitions: how to declare class constructors - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742232663a2437506.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论