Microsoft/TypeScript

Property inherited from mixin of function type with parameter of type "this" incorrectly extends base class static side

Open

#53 088 ouverte le 4 mars 2023

Voir sur GitHub
 (5 commentaires) (0 réactions) (0 assignés)TypeScript (6 726 forks)batch import
BugDomain: This-TypingHelp Wanted

Métriques du dépôt

Stars
 (48 455 stars)
Métriques de merge PR
 (Merge moyen 6j 17h) (9 PRs mergées en 30 j)

Description

Bug Report

🔎 Search Terms

mixin, this type, function parameter

🕗 Version & Regression Information

  • This changed between versions 4.8.4 and 4.9.5

⏯ Playground Link

Playground link

💻 Code

declare const Type: FunctionConstructor;
declare interface Type<T> extends Function {
    new (...args: any[]): T;
}

class Dummy{}

function Mix<TC extends Type<{}>>(Base: TC){
    abstract class TestBase extends Base {

        abstract testFunc: (self: this) => void;

        protected abstract SomeMethod(self: this): string;

        otherMethod(): string{
            return this.SomeMethod(this);
        }

    }
    return TestBase;
}

const mixed = Mix(Dummy);

//error *not* expected here
class Test extends mixed{
    testFunc = this.testUp

    testProp: string = 'test';

    protected SomeMethod(self: this): string {
        return self.testProp;
    }

    private testUp(self: this): void {
        self.testProp;
    }
}

const testTest = new Test();
//error expected here
testTest.testFunc = (self: typeof mixed) => {}

🙁 Actual behavior

The code fails to compile with the error


Class static side 'typeof Test' incorrectly extends base class static side '{ prototype: Mix.TestBase; apply(this: Function, thisArg: any, argArray?: any): any; call(this: Function, thisArg: any, ...argArray: any[]): any; ... 6 more ...; [Symbol.hasInstance](value: any): boolean; } & typeof Dummy'. Types of property 'prototype' are incompatible. Type 'Test' is not assignable to type 'Mix.TestBase & Dummy'. Type 'Test' is not assignable to type 'Mix.TestBase'. Types of property 'testFunc' are incompatible. Type '(self: Test) => void' is not assignable to type '(self: Mix.TestBase) => void'. Types of parameters 'self' and 'self' are incompatible. Type 'Mix.TestBase' is missing the following properties from type 'Test': testProp, testUp Type '(self: typeof mixed) => void' is not assignable to type '(self: Test) => void'. Types of parameters 'self' and 'self' are incompatible. Type 'Test' is not assignable to type '((abstract new (...args: any[]) => Mix.TestBase) & { prototype: Mix.TestBase; apply(this: Function, thisArg: any, argArray?: any): any; ... 7 more ...; [Symbol.hasInstance](value: any): boolean; }) & typeof Dummy'. Type 'Test' is missing the following properties from type '(abstract new (...args: any[]) => Mix.TestBase) & { prototype: Mix.TestBase; apply(this: Function, thisArg: any, argArray?: any): any; ... 7 more ...; [Symbol.hasInstance](value: any): boolean; }': prototype, apply, call, bind, and 5 more.


🙂 Expected behavior

Prior to 4.9.5 this error did not occur. It seems to me that this is treating methods differently than function valued properties which is why I included "SomeMethod" in my example. Both "SomeMethod" and "testFunc" contain a function which takes a parameter of type "this" yet one produces the error while the other does not. Perhaps there is a good reason for this which I am missing but to my eye the two scenarios (method vs property) are very similar if not the same.

Swapping from 4.9.5 to 4.8.4 in the playground makes the error go away.

Here is a playground with a similar scenario without mixin:

Playground link

My actual use case is for the function valued property to be protected. I understand why the following causes an error

Playground link

but it seems to me that even that could be alleviated if the property is protected

Playground link

Guide contributeur