How to use class returned by a function in type annotations?

后端 未结 2 787
你的背包
你的背包 2021-01-14 20:32

This is the simplified code

export interface MyInterface{
    // Have no idea why I need an export here.
    // If I remove it, it gives
    // semantic erro         


        
2条回答
  •  执念已碎
    2021-01-14 21:30

    TS2451 Cannot redeclare block-scoped variable 'createClass'

    This error occurs because you have createClass function somewhere else in your ambient environment. Ambient means, not scoped by modules. Writing at least one import or export scopes your file, i.e. makes it a module. Instead of exporting random interface you could just do this: export {} in the end of the file.

    const MyClass = createClass(); 
    const mc: MyClass
    

    In the above code you are mixing types with values. Specifically, MyClass is a value here and you are trying to use it as a type.

    Overall, you are doing weird things by returning a class by a function. I know you might have your own reasons for that, but normally you should write that code like this:

    class MyClass {}
    const mc: MyClass = new MyClass() // thought the type annotation is not necessary
    

提交回复
热议问题