简体   繁体   English

获取TypeScript中嵌套class的类型

[英]Get the type of a nested class in TypeScript

I'm using nested classes in TypeScript by using the following code:我通过使用以下代码在 TypeScript 中使用嵌套类

class Parent {
  private secret = 'this is secret'
  
  static Child = class {
    public readSecret(parent: Parent) {
      return parent.secret
    }
  }
}

This is based on the following answer .这是基于以下答案 It allows my Child class to access private properties of the Parent class.它允许我的Child class 访问Parent class 的私有属性。

I want to type a variable with the nested class type, I started naïvely with the following code:我想用嵌套的 class 类型键入一个变量,我天真地开始使用以下代码:

type NestedType = Parent.Child
const child: NestedType = new Parent.Child()

But I get the following error:但我收到以下错误:

'Parent' only refers to a type, but is being used as a namespace here. ts(2702)

I tried to use the typeof operator:我尝试使用typeof运算符:

type NestedType = typeof Parent.Child
const child: NestedType = new Parent.Child()

But it doesn't work:但它不起作用:

Property 'prototype' is missing in type '(Anonymous class)' but required in type 'typeof (Anonymous class)'. ts(2741)

Finally I was able to make it work:最后我能够让它工作:

const nestedTemplate = new Parent.Child()
type NestedType = typeof nestedTemplate
const child: NestedType = new Parent.Child()

However, this code creates a useless instance of Child .但是,这段代码创建了一个无用的Child实例。 Is this a limitation of the TS language?这是TS语言的限制吗?

FYI, I also tried by using namespaces:仅供参考,我还尝试使用名称空间:

class Parent {
  private secret = 'this is secret'
}

namespace Parent {
  export class Child {
    public readSecret(parent: Parent) {
      return parent.secret
    }
  }
}

But then the Child class is no longer able to access the private properties of the Parent class:但是随后Child class 不再能够访问Parent class 的私有属性:

Property 'secret' is private and only accessible within class 'Parent'. ts(2341)

Using the typeof child prototype should be enough to resolve your typings trouble.使用typeof子原型应该足以解决您的打字问题。

class Parent {
  private secret = 'this is secret'

  static Child = class {
    public readSecret(parent: Parent) {
      return parent.secret
    }
  }
}

const child: typeof Parent.Child.prototype = new Parent.Child();
console.log(child.readSecret(new Parent()));

You can test it there Playground Link你可以在那里测试它Playground Link

That way you do not have to instantiate any intermediary classes.这样您就不必实例化任何中间类。

You can access the Child class via the interface property access notation ( foo["bar"] ):您可以通过接口属性访问表示法 ( foo["bar"] ) 访问Child class:

class Parent {
  private secret = 'this is secret'
  
  static Child = class {
    public readSecret(parent: Parent) {
      return parent.secret
    }
  }
}

type ParentChildProto = (typeof Parent)["Child"];

const child: InstanceType<ParentChildProto> = null!; // same as assigning to `new Parent.Child()`
child.readSecret(new Parent());

TypeScript Playground Link TypeScript 游乐场链接

Edit: Use built-in InstanceType see comment.编辑:使用内置的InstanceType见评论。

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

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