Typescript: typeof this.xxx给出“期望的标识符”错误。

创建于 2014-12-24  ·  19评论  ·  资料来源: microsoft/TypeScript

image

这是设计使然还是错误?

Suggestion help wanted

最有用的评论

如今,@ sam-s4s让您怀念能够写CMultiDataset<this["thing"]>的想法。 与typeof不同,索引的附件_do_与this (甚至是一个多态的)一起使用。

我刚刚尝试过,但是它在TypeScript 3.4.4中不起作用,@ sam-s4s的一种有效( ClassName['thing'] )。 我认为this应该起作用,而不是只能使用“ ClassName”可能更有意义?

所有19条评论

我不会说这两个...这只是语法错误...请尝试“ var copy = typeof(this.data = {});” ...说了这一点,我不会使用此代码...这很令人困惑。 。

@giancarloa嗯,您不应该添加这些括号。 上面的代码只是一个最小问题触发器,而不是生产中的代码,在某些情况下,您可能希望使用这样的代码。

在这里,如果您将this用作除此以外的其他值,那会很好,并且不会报告任何错误。 例如

class Test {
    static data = {};
    constructor() {
        var copy: typeof Test.data = {};
    }
}

根据规格

_TypeQueryExpression:_
_标识符_
_TypeQueryExpression_ . _IdentifierName_

因此,这是预期的。

我认为不允许的原因是这样的

function foo() {
    var x: typeof this.x;
}

是完全有效的,因为this或多或少具有any 。 因此,尽管在类中可能有意义,但在其他任何情况下除非我们具有#229,否则它没有任何意义。

@DanielRosenwasser OO嗯...然后希望您可以考虑更新规格。 大声笑。

这是要考虑的事情; 如果您的成员不是私人成员,那么与此同时,您可以轻松解决以下问题。

self = this;
var x: typeof self.data;

等待此功能。
我认为最简单的情况应该得到支持
var a:typeof this.x = 1;

递归typeof成员应该导致错误。

谢谢。

如果我们更改语法,似乎这样就应该起作用。

@RyanCavanaugh ,但是在我给的上下文中允许它合适吗? 它将不可避免地导致人们这样做,并获得any作为他们的类型。

语法上正确的意思并不一定意味着它应该真正有意义,IMO。
但是,可以为此原因提出警告。

我们已经让你写typeof foo.bar其中foo的类型为any ,我们已经让你写this.x其他点表达职位时的类型thisany 。 我不明白为什么他们在typeof this.x处的交集应该值得例外。

只是添加另一个用例。 前一段时间,我为lodash定义了类型,某些受支持的签名实际上是其他方法的别名。 我已经这样声明了它们:

interface LoDashArrayWrapper<T> {
    rest(): LoDashArrayWrapper<T>;
    rest(
        callback: ListIterator<T, boolean>,
        thisArg?: any): LoDashArrayWrapper<T>;
    rest(n: number): LoDashArrayWrapper<T>;
    rest(pluckValue: string): LoDashArrayWrapper<T>;
    rest(whereValue: {}): LoDashArrayWrapper<T>;

    drop: typeof rest;

    tail: typeof rest;
}

这在编译器的1.0版中正常工作。 但是它不再编译。 而且,除了为每个别名复制整个签名集之外,我看不到任何其他选项,因为我想不到可以表达这一点的TypeQueryExpression。

@juanevp函数界面如何?

interface LoDashArrayWrapper<T> {
    rest: LoDashArrayWrapperOperation<T>;
    drop: LoDashArrayWrapperOperation<T>;
    tail: LoDashArrayWrapperOperation<T>;
}

interface LoDashArrayWrapperOperation<T> {
    (): LoDashArrayWrapper<T>;
    (
        callback: ListIterator<T, boolean>,
        thisArg?: any): LoDashArrayWrapper<T>;
    (n: number): LoDashArrayWrapper<T>;
    (pluckValue: string): LoDashArrayWrapper<T>;
    (whereValue: {}): LoDashArrayWrapper<T>;
}

已批准。 应该非常容易修复吗? 确保这不会导致_this = this在箭头函数中发出

有关系吗我得到[ts] Identifier expected.与以下内容:


class UserState {
    <strong i="7">@observable</strong> state = {
        name : "",
        id : "",
    };

    <strong i="8">@action</strong>
    changeUser(user: typeof this.state) { // Error is here
        Object.assign(this.state, user);
    }
}

export const userState = new UserState();

在上述情况下能够使用this.state会很有用。

嗯,我刚遇到这个问题:(

我在课堂上定义一个对象。
然后,我定义另一个具有类型参数的对象,该参数必须是上述类型的参数。

thing = {
    a: 1,
    b: 2
};
multi: CMultiDataset<typeof this.thing>;

在这种情况下,仅将事物更改为静态变量并说typeof ClassName.thing是不合适的,因为在每种情况下它都会有所不同。 另外, multi必须是一个类变量,因此在类声明时必须具有其类型。

@RyanCavanaugh @DanielRosenwasser @mhegazy听起来像这样相对容易解决? 看起来,尽管多年来,它已经失去了人们的关注...实现它的任何机会?

如今,@ sam-s4s让您怀念能够写CMultiDataset<this["thing"]>的想法。 与typeof不同,索引的附件_do_与this (甚至是一个多态的)一起使用。

谢谢@weswigham-我确实发现我可以做CMultiDataset<ClassName['thing']> ,这基本上使我摆脱了麻烦:)

我找不到使用this任何组合,但是可以使用...

如今,@ sam-s4s让您怀念能够写CMultiDataset<this["thing"]>的想法。 与typeof不同,索引的附件_do_与this (甚至是一个多态的)一起使用。

我刚刚尝试过,但是它在TypeScript 3.4.4中不起作用,@ sam-s4s的一种有效( ClassName['thing'] )。 我认为this应该起作用,而不是只能使用“ ClassName”可能更有意义?

如果我有类似的东西
打字稿
public groupByTypes = ['None','Foo','Bar']作为常量;

public groupBy:typeof groupByTypes [number];
`` What is the proposed workaround? And I cannot make groupByTypes`静态

此页面是否有帮助?
0 / 5 - 0 等级