microsoft / microsoft/TypeScript

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

Open
#53,088 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Domain: This-Typing Help Wanted
Dominant language
Go
Stars
111k
Forks
14.4k
Avg merge
1d 19h
Merged PRs (30d)
117

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the reported mixin example in the TypeScript Playground using 4.9.5, then compare it with 4.8.4 and the nightly version. Trace the compiler's handling of function-valued properties and methods with a this parameter during static-side inheritance checks. Done means the valid Test extends mixed declaration compiles while the marked testFunc assignment remains an error.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.