Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.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_Generics_Constructor - Fatal编程技术网

如何将类构造函数参数作为文本字符串与TypeScript一起重用?

如何将类构造函数参数作为文本字符串与TypeScript一起重用?,typescript,class,generics,constructor,Typescript,Class,Generics,Constructor,我怎么能做到 类型模块={ 创建:编号; } 课例{ 建造师( 公共只读moduleName:string ) { } getModule=(模块:{ [this.moduleName]:模块//模块[this.moduleName] } 属性错误为“类型文字中的计算属性名称必须引用类型为文字类型或“唯一符号”类型的表达式。” 操场是这就是你想要做的吗 type Module = { created: number; } class Test<T extends string&

我怎么能做到

类型模块={
创建:编号;
}
课例{
建造师(
公共只读moduleName:string
) { }
getModule=(模块:{
[this.moduleName]:模块//模块[this.moduleName]
}
属性错误为“类型文字中的计算属性名称必须引用类型为文字类型或“唯一符号”类型的表达式。”


操场是

这就是你想要做的吗

type Module = {
    created: number;
}

class Test<T extends string> {
    constructor(
        public readonly moduleName: T
    ) { }

    getModule = (modules: {
        [key: string]: Module
    }): Module => modules[this.moduleName]
}
type Module = {
    created: number;
}

interface ModuleCache { [key: string]: Module }

class Test<T extends string> {
    constructor(
        public readonly moduleName: T
    ) { }

    getModule = (modules: ModuleCache): Module =>
        modules[this.moduleName]
}