镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Cannot use 'this' type in type argument on base types #7289

Description

interface Foo<T> {
    thing: T;
}

interface Bar extends Foo<this> {
    thing: this;
}
class Foo<T> {
    thing: T;
}

class Bar extends Foo<this> {
    thing: this;
}
interface Foo<T> {
    thing: T;
}

class Bar implements Foo<this> {
    thing: this;
}

These examples have an error on the 'this' type argument of Foo:

TS2526  A 'this' type is available only in a non-static member of a class or interface.

Since an object type cannot reference a type parameter directly within a heritage clause, we shouldn't be giving this error.

Activity

  1. mhegazy commented on Apr 26, 2016

    @mhegazy
    Contributor

    The class is by design, as the type parameter could be used in the constructor. but the interface seems reasonable.

  2. DanielRosenwasser commented on Apr 26, 2016

    @DanielRosenwasser
    MemberAuthor

    as the type parameter could be used in the constructor

    Can you elaborate what you mean? I'm not necessarily seeing the issue in that case.

  3. kitsonk commented on May 17, 2016

    @kitsonk
    Contributor

    Related to #6223. While subtly different, I suspect they are in the same problem space.

  4. dead-claudia commented on Feb 14, 2017

    @dead-claudia

    Well...we could use it for Mithril.js types, where we were looking to add class support. We could avoid creating an entire boilerplate type by just allowing this as a generic parameter in the implements clause. Just thought I'd bring that up.

    (Oh, and this would also help quite a bit from the other end.)

  5. RyanCavanaugh commented on Jul 22, 2020

    @RyanCavanaugh
    Member

    Seems to not be a very popular scenario

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    In DiscussionNot yet reached consensusSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions