Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将未初始化的类应用于Typescript中的对象?_Typescript_Class - Fatal编程技术网

如何将未初始化的类应用于Typescript中的对象?

如何将未初始化的类应用于Typescript中的对象?,typescript,class,Typescript,Class,我希望这个代码示例足够清楚我要做的事情 请注意,这是一个简化的测试用例,用于演示我遇到的核心问题。我不想知道如何使这个例子更简单。我想知道如何阻止TypeScript抱怨。我也不想使用any类型 // start with a class class HelloWorld { hello = 'hello' world(): string { return this.hello + ' world' } } // Create an interface

我希望这个代码示例足够清楚我要做的事情

请注意,这是一个简化的测试用例,用于演示我遇到的核心问题。我不想知道如何使这个例子更简单。我想知道如何阻止TypeScript抱怨。我也不想使用
any
类型

// start with a class
class HelloWorld {
    hello = 'hello'

    world(): string {
        return this.hello + ' world'
    }
}

// Create an interface for an object
interface Greeting {
    Hello: HelloWorld
}

// The interface is applied to an object
// The class is applied to a value inside the object
// Note that it is an UNINITIALISED class
const greet: Greeting = {
    // ERROR: Type 'typeof HelloWorld' is missing the following properties
    // from type 'HelloWorld': hello, world ts(2739)
    Hello: HelloWorld,
}

// Later in the code you initialise it like this
// ERROR: This expression is not constructable.
// Type 'HelloWorld' has no construct signatures.ts(2351)
const greeting = new greet.Hello()

console.log(greeting.world()) // "hello world"
这里将普通类视为实例的接口。它仅适用于使用
new
关键字声明的实例。要在接口中正确使用类类型,请将类转换为类型或接口,以便TypeScript知道您是在推断类本身,而不是它的实例

下面是如何为类和类实例声明接口

interface Greeting {
    // declaring type for the class itself
    Hello: typeof HelloWorld,

    // declaring the type for it's instance
    hello: HelloWorld
}

const greet: Greeting = {
    // Assigning the class itself
    Hello: HelloWorld,

    // Assigning an instances of HelloWorld
    hello: new HelloWorld()
}

const greeting = new greet.Hello()

// `greeting` is now identical to `greet.hello`

这里将普通类视为实例的接口。它仅适用于使用
new
关键字声明的实例。要在接口中正确使用类类型,请将类转换为类型或接口,以便TypeScript知道您是在推断类本身,而不是它的实例

下面是如何为类和类实例声明接口

interface Greeting {
    // declaring type for the class itself
    Hello: typeof HelloWorld,

    // declaring the type for it's instance
    hello: HelloWorld
}

const greet: Greeting = {
    // Assigning the class itself
    Hello: HelloWorld,

    // Assigning an instances of HelloWorld
    hello: new HelloWorld()
}

const greeting = new greet.Hello()

// `greeting` is now identical to `greet.hello`


耶!谢谢DI只是想特别指出它是一个未调用的类,因为我能找到的唯一答案涉及调用类。所谓调用类,你是指它的实例吗?
new greet.Hello()
就是我所说的“调用”类。我需要重新表述这个问题吗?更新了我的答案,并解释了类推理和实例推理之间的区别。希望将来也能帮助别人!谢谢DI只是想特别指出它是一个未调用的类,因为我能找到的唯一答案涉及调用类。所谓调用类,你是指它的实例吗?
new greet.Hello()
就是我所说的“调用”类。我需要重新表述这个问题吗?更新了我的答案,并解释了类推理和实例推理之间的区别。希望它将来也能帮助别人