Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handbook applyMixins code does not compile #682

Closed
blimmer opened this issue Apr 9, 2020 · 3 comments
Closed

Handbook applyMixins code does not compile #682

blimmer opened this issue Apr 9, 2020 · 3 comments

Comments

@blimmer
Copy link

blimmer commented Apr 9, 2020

TypeScript Version: 3.8.3

Search Terms:

  • Handbook Mixin
  • Argument of type 'PropertyDescriptor | undefined' is not assignable to parameter of type 'PropertyDescriptor & ThisType'.

Expected behavior:
I expected the example in the Handbook to compile without problems.

Actual behavior:
Copying the suggested code from the docs/handbook failed to compile

Related Issues:

  • I couldn't find any

Code

// See https://www.typescriptlang.org/docs/handbook/mixins.html
// Error is:
// Argument of type 'PropertyDescriptor | undefined' is not assignable to parameter of type 'PropertyDescriptor & ThisType<any>'.
//  Type 'undefined' is not assignable to type 'PropertyDescriptor & ThisType<any>'.
//    Type 'undefined' is not assignable to type 'PropertyDescriptor'.(2345)
function applyMixins(derivedCtor: any, baseCtors: any[]) {
  baseCtors.forEach((baseCtor) => {
      Object.getOwnPropertyNames(baseCtor.prototype).forEach((name) => {
      Object.defineProperty(derivedCtor.prototype, name, Object.getOwnPropertyDescriptor(baseCtor.prototype, name));
    });
  });
}
Output
"use strict";
// See https://www.typescriptlang.org/docs/handbook/mixins.html
// Error is:
// Argument of type 'PropertyDescriptor | undefined' is not assignable to parameter of type 'PropertyDescriptor & ThisType<any>'.
//  Type 'undefined' is not assignable to type 'PropertyDescriptor & ThisType<any>'.
//    Type 'undefined' is not assignable to type 'PropertyDescriptor'.(2345)
function applyMixins(derivedCtor, baseCtors) {
    baseCtors.forEach((baseCtor) => {
        Object.getOwnPropertyNames(baseCtor.prototype).forEach((name) => {
            Object.defineProperty(derivedCtor.prototype, name, Object.getOwnPropertyDescriptor(baseCtor.prototype, name));
        });
    });
}
Compiler Options
{
  "compilerOptions": {
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "strictBindCallApply": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "useDefineForClassFields": false,
    "alwaysStrict": true,
    "allowUnreachableCode": false,
    "allowUnusedLabels": false,
    "downlevelIteration": false,
    "noEmitHelpers": false,
    "noLib": false,
    "noStrictGenericChecks": false,
    "noUnusedLocals": false,
    "noUnusedParameters": false,
    "esModuleInterop": true,
    "preserveConstEnums": false,
    "removeComments": false,
    "skipLibCheck": false,
    "checkJs": false,
    "allowJs": false,
    "declaration": true,
    "experimentalDecorators": false,
    "emitDecoratorMetadata": false,
    "target": "ES2017",
    "module": "ESNext"
  }
}

Playground Link: Provided

@blimmer
Copy link
Author

blimmer commented Apr 9, 2020

It looks like we might just need a guard, like this:

function applyMixins(derivedCtor: any, baseCtors: any[]) {
  baseCtors.forEach((baseCtor) => {
    Object.getOwnPropertyNames(baseCtor.prototype).forEach((name) => {
      const baseCtorName = Object.getOwnPropertyDescriptor(baseCtor.prototype, name);
      if (!baseCtorName) {
        return;
      }
      Object.defineProperty(derivedCtor.prototype, name, baseCtorName);
    });
  });
}

@orta orta transferred this issue from microsoft/TypeScript Jun 17, 2020
@orta
Copy link
Contributor

orta commented Jul 16, 2020

Fixed by my re-write of the mixins docs for the new site #719

@orta orta closed this as completed Jul 16, 2020
@cx1111
Copy link

cx1111 commented Oct 12, 2020

Hi @orta , it appears that #719 did not implement the typeguard from @blimmer above.

https://github.com/microsoft/TypeScript-Website/pull/719/files#diff-4f2ae2b0dc666e35310daac6f74a190dR193

The function from the latest version of the handbook still shows the same error as specified in the OP.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants