You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何获取TypeScript私有构造函数单例类的实例类型?

问题:获取带私有构造函数类的实例类型

我有一个带私有构造函数的单例类:

class Singleton {
  private static instance: Singleton;

  private constructor() {
    // 私有构造函数,防止外部实例化
  }

  public static getInstance(): Singleton {
    if (!Singleton.instance) {
      Singleton.instance = new Singleton();
    }
    return Singleton.instance;
  }

  someMethod(): void {
    console.log("Singleton method called");
  }
}

我知道TypeScript内置了InstanceType类型,但它无法用于带私有构造函数的类:

// 报错:Cannot assign a 'private' constructor type to a 'public' constructor type
type SingletonType = InstanceType<typeof Singleton>;

能否创建一个自定义类型来返回带私有构造函数类的实例类型?


补充需求
我需要将该单例传入另一个泛型类的构造函数中:

class BaseEntity<T extends typeof Singleton = typeof Singleton> {
    private singletonInstance: InstanceType<T>;
    
    constructor(instance: InstanceType<T>) {        
        this.singletonInstance = instance;        
    }
}

由于Singleton类的构造函数是私有的,使用InstanceType<T>时会报错,请问能否创建支持私有构造函数类的自定义泛型类型来获取其实例类型?


解决方案

方法1:自定义替代InstanceType的类型

可以利用TypeScript条件类型,直接提取类的实例类型,避开构造函数的访问权限检查:

type PrivateInstanceType<T extends abstract new (...args: any) => any> = 
  T extends abstract new (...args: any) => infer R ? R : never;

用这个类型替代InstanceType,就能正常获取私有构造函数类的实例类型:

type SingletonType = PrivateInstanceType<typeof Singleton>; // 正确,类型为Singleton

方法2:调整泛型类的约束逻辑

针对你的BaseEntity场景,也可以直接约束实例类型而非构造函数类型,写法更简洁直观:

class BaseEntity<T = Singleton> {
    private singletonInstance: T;
    
    constructor(instance: T) {        
        this.singletonInstance = instance;        
    }
}

// 使用示例
const singleton = Singleton.getInstance();
const entity = new BaseEntity(singleton); // 类型推导正常,无报错

如果需要保留对构造函数类型的约束(比如确保传入的是单例类),可以结合自定义的PrivateInstanceType:

class BaseEntity<T extends abstract new (...args: any) => any = typeof Singleton> {
    private singletonInstance: PrivateInstanceType<T>;
    
    constructor(instance: PrivateInstanceType<T>) {        
        this.singletonInstance = instance;        
    }
}

// 使用示例
const entity = new BaseEntity(Singleton.getInstance());

原理说明

TypeScript内置的InstanceType在处理私有/受保护构造函数的类时,会因为构造函数的访问权限限制触发报错。而自定义的PrivateInstanceType通过条件类型直接提取构造函数返回的实例类型,绕开了对构造函数访问权限的检查,因此可以正常工作。

内容的提问来源于stack exchange,提问作者Nolesh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 04:45:25