简体   繁体   English

为什么我不能在打字稿类中定义接口或类型

[英]why i can't define interface or type inside typescript class

I want to define interface |我想定义interface | type inside the typescript class here is the code:在 typescript 类中type这里的代码:

class MyClass {
    interface IClass {
        name: string,
        id: string
    }
}

but I'm getting this error: Unexpected token. A constructor, method, accessor, or property was expected.但我收到此错误: Unexpected token. A constructor, method, accessor, or property was expected. Unexpected token. A constructor, method, accessor, or property was expected.

Exactly want to achieve:-正是想要实现:-

I'm making a framework where the user to extend the base class Randoms and override a few methods of the base but I'm not getting any type of intelligence in the child class.我正在制作一个框架,用户可以在其中扩展基类Randoms并覆盖基类的一些方法,但我没有在子类中获得任何类型的智能。
here is the code:这是代码:

abstract class RandomsRoute {
   public get (req:Resquest, res:Response): Promise <void> { res.send ('') }
}

// client side

import RandomsRoute, { Request, Response } from '@my-pkg'

class Client extends RandomsRoute {
   // public get (req, res) {res.send('client side')} // error here
   public get (req: Request, res: Response): Promise <void> { res.send ('') }
}

here the这里的

{ Request, Response } from '@my-pkg'

I don't want the user to make lots of imports can we simplify this anymore or maybe provide some better APIs to user?

Tanks in Advance坦克提前

It is not currently possible to declare types or interfaces directly in the scope of a class body.目前无法在类主体的范围内直接声明类型或接口。 There is a feature request for it at microsoft/TypeScript#7061 , but it's not part of the language.microsoft/TypeScript#7061有一个功能请求,但它不是语言的一部分。 Even if it were, it's not clear that you'd be able to export the types out of the class.即使是,也不清楚您是否能够将类型导出到类之外。 For example, you can define types in functions, but the definitions of these types are invisible outside the function:例如,您可以在函数中定义类型,但这些类型的定义在函数外是不可见的:

function foo() {
  interface Foo {
    a: string,
    b: number;
  }
}

const f: Foo = {a: "", b: 1}; // error!
// ----> ~~~ cannot find name Foo

const g: foo.Foo = {a: "", b: 1}; // error!
// ----> ~~~ cannot find namespace foo

function foo() {
  export interface Foo { // error!
  //~~~~ <-- modifiers cannot appear here.
    a: string,
    b: number;
  }
}

So you probably don't really want to define your interface inside the class.所以你可能真的不想类中定义你的接口。


Instead, what you can do, as mentioned in this comment on microsoft/TypeScript#7061 is merge the class with a namespace from which you export your type:相反,正如在 microsoft/TypeScript#7061 上的评论中提到的,您可以做的是类与您从中导出类型的namespace合并:

export class MyClass {

}
export declare namespace MyClass {
  export interface IClass {
    name: string,
    id: string
  }
}

Then, in your other file, you can import MyClass and get both the class and the namespace:然后,在您的其他文件中,您可以导入MyClass并获取类和命名空间:

import { MyClass } from '@my-pkg'
const c: MyClass = new MyClass();
const i: MyClass.IClass = { name: "", id: "" };

Playground link to code 游乐场代码链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM