首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >闭包编译器不再能够确定类型。

闭包编译器不再能够确定类型。
EN

Stack Overflow用户
提问于 2018-08-08 14:12:05
回答 1查看 58关注 0票数 0

在我的大多数项目中,我一直在使用Google的闭包编译器,其中有几个是100%输入的高级模式。

虽然我的一个项目不再被说成是100%的类型,但是我收到了一些我以前没有得到它们的警告,而且我似乎无法弄清楚为什么。这就是我得到的信息

代码语言:javascript
复制
WARNING - could not determine the type of this expression
                                                v['children'].map(v => new _ChildLineItem(v['child'], v['option'], v['quantity'], v['price'])))))),
                                                                                                                                  ^

还有42个类似的警告,都是相同的代码,我在这里有

代码语言:javascript
复制
    /**
     * @constructor
     * @param {Array<!_Quote>} quotes
     * @param {string} email
     * @param {?string} quoteid
     */
    function _GetQuotes(quotes, email, quoteid) {
        this.quotes = quotes;
        this.email = email;
        this.quoteid = quoteid;
    }

    /**
     * @constructor
     * @param {string} quoteid
     * @param {boolean} shipping
     * @param {Array<!_Proof>} proofs
     * @param {Array<!_LineItem>} lineitems
     */
    function _Quote(quoteid, shipping, proofs, lineitems) {
        this.quoteid = quoteid;
        this.shipping = shipping;
        this.proofs = proofs;
        this.lineitems = lineitems;
    }

    /**
     * @constructor
     * @param {string} number
     * @param {string} main
     * @param {string} thumbnail
     */
    function _Proof(number, main, thumbnail) {
        this.number = number;
        this.main = main;
        this.thumbnail = thumbnail;
    }

    /**
     * @constructor
     * @param {string} name
     * @param {number} quantity
     * @param {number} price
     * @param {Array<!_ChildLineItem>} children
     * */
    function _LineItem(name, quantity, price, children) {
        this.name = name;
        this.quantity = quantity;
        this.price = price;
        this.children = children;
    }

    /**
     * @constructor
     * @param {string} child
     * @param {string} option
     * @param {number} quantity
     * @param {number} price
     * */
    function _ChildLineItem(child, option, quantity, price) {
        this.child = child;
        this.option = option;
        this.quantity = quantity;
        this.price = price;
    }

    Ajax({
        url: '/ajax/getquotes',
        data: Data,
        success: function (/** !_GetQuotes */ data) {
            var d = new _GetQuotes(
                data['quotes'].map(v => new _Quote(v['quoteid'], v['shipping'],
                    v['proofs'].map(v => new _Proof(v['number'], v['main'], v['thumbnail'])),
                    v['lineitems'].map(v => new _LineItem(v['name'], v['quantity'], v['price'],
                        v['children'].map(v => new _ChildLineItem(v['child'], v['option'], v['quantity'], v['price'])))))),
                data['email'], data['quoteid']);
    ...

我可以重写闭包来指定对象的类型,如下所示

代码语言:javascript
复制
v['children'].map(function( /** !_ChildLineItem */ v) { new _ChildLineItem(v['child'], v['option'], v['quantity'], v['price'])}

但是,它难道不能从构造函数定义中找出这一点吗?

实际上,我把它们都写成这样根本不起作用

代码语言:javascript
复制
var d = new _GetQuotes(
    data['quotes'].map((/** !_Quote */ v) => new _Quote(v['quoteid'], v['shipping'],
        v['proofs'].map((/** !_Proof */ v) => new _Proof(v['number'], v['main'], v['thumbnail'])),
        v['lineitems'].map((/** !_LineItem */ v) => new _LineItem(v['name'], v['quantity'], v['price'],
            v['children'].map((/** !_ChildLineItem */ v) => new _ChildLineItem(v['child'], v['option'], v['quantity'], v['price'])))))),
    data['email'], data['quoteid']);

带着这个警告

代码语言:javascript
复制
WARNING - could not determine the type of this expression
                                                v['children'].map((/** !_ChildLineItem */ v) => new _ChildLineItem(v['child'], v['option'], v['quantity'], v['price'])))))),
                                                ^^^^^^^^^^^^^^^^^
EN

回答 1

Stack Overflow用户

发布于 2018-09-13 19:48:57

您所面临的问题是编译器通常以不同的方式对待计算和点属性访问。计算的属性访问被视为“未知”类型,除非您将[]运算符赋予类型签名(即类实现IArrayLike,如Array或IObject2用于映射类对象)。

尽管编译器可以理解x.foo和x‘’foo‘是引用相同属性的,但它目前没有这样做。

1

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51748851

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档