Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/431.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

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
Javascript 如何进行运行时强制转换_Javascript_Typescript_Casting - Fatal编程技术网

Javascript 如何进行运行时强制转换

Javascript 如何进行运行时强制转换,javascript,typescript,casting,Javascript,Typescript,Casting,我在用TypeScript编写的web应用程序上。在应用程序的一部分中,用户可以编写额外的JavaScript函数,该函数将在运行时解析(新函数(函数为字符串))。代码将返回一个我在TypeScript中定义为类脚本的对象。此脚本对象应包含特定函数,否则无效。也就是说,如果用户没有实现一个或多个函数,我想抛出一个异常 Typescript强制转换无法完成此操作,因为它是编译时强制转换 我考虑给Script对象一个构造函数,该构造函数接受已解析的对象(根据键/值,该对象应该已经是Script对象)

我在用TypeScript编写的web应用程序上。在应用程序的一部分中,用户可以编写额外的JavaScript函数,该函数将在运行时解析(
新函数(函数为字符串)
)。代码将返回一个我在TypeScript中定义为
类脚本
的对象。此脚本对象应包含特定函数,否则无效。也就是说,如果用户没有实现一个或多个函数,我想抛出一个异常

Typescript强制转换无法完成此操作,因为它是编译时强制转换

我考虑给
Script
对象一个构造函数,该构造函数接受已解析的对象(根据键/值,该对象应该已经是
Script
对象),并检查构造函数中的对象是否缺少属性。 大概是这样的: (这不起作用,它只应表明想法)


但是有没有更好的方法可以做到这一点呢?

你不能使用它,因为:

class A {
    member1: string;
    member2: number;
    member3: boolean;

    constructor() {
        this.member3 = true;
    }
}
汇编成:

var A = (function () {
    function A() {
        this.member3 = true;
    }
    return A;
}());
如您所见,
member1
member2
不是编译的js版本的一部分。
您需要跟踪运行时所需的属性,例如:

class Script {
    getDefaultValue: any;
    isAvailable: (containerSetId: number) => boolean;
    isValid: (value: any) => boolean;
    isInRange: (value: any) => any;
    isDataFormat: (value: any) => boolean;

    required = [
        "getDefaultValue",
        "isAvailable",
        "isValid",
        "isInRange",
        "isDataFormat"
    ]

    constructor(object: Script) {
        this.getDefaultValue = object.getDefaultValue;
        this.isAvailable = object.isAvailable;
        this.isValid = object.isValid;
        this.isInRange = object.isInRange;
        this.isDataFormat = object.isDataFormat;

        for (let propertie in this.required) {
            if (!this[propertie] || typeof this[propertie] !== "function") {
                throw new Error(propertie+ ' is missing.');
            }
        }
    }
}

不,此代码无效。我加了一张便条。这是一种伪代码,你想检查这个循环,上面所有的属性是设置的还是未定义的?是的,如果一个属性未定义,抛出一个异常,因为给定的对象显然不是你所处的类型
Script
。在我看来,你所能做的就是进行一系列的
typeof
instanceof
检查。谢谢你的推理。那很好用。这个
!此[属性]=“函数”
的类型需要使用传统括号=>
!(此[属性]=“功能”的类型”)
My bad。我将它更改为此[属性]的
类型!==“函数”
是的,实际上更好:D
class Script {
    getDefaultValue: any;
    isAvailable: (containerSetId: number) => boolean;
    isValid: (value: any) => boolean;
    isInRange: (value: any) => any;
    isDataFormat: (value: any) => boolean;

    required = [
        "getDefaultValue",
        "isAvailable",
        "isValid",
        "isInRange",
        "isDataFormat"
    ]

    constructor(object: Script) {
        this.getDefaultValue = object.getDefaultValue;
        this.isAvailable = object.isAvailable;
        this.isValid = object.isValid;
        this.isInRange = object.isInRange;
        this.isDataFormat = object.isDataFormat;

        for (let propertie in this.required) {
            if (!this[propertie] || typeof this[propertie] !== "function") {
                throw new Error(propertie+ ' is missing.');
            }
        }
    }
}