Giter Club home page Giter Club logo

Comments (3)

RyanCavanaugh avatar RyanCavanaugh commented on May 1, 2024

Subclassing doesn't guarantee subtyping. Anything declared in the derived class might illegally override something in the base class

from typescript.

zthun avatar zthun commented on May 1, 2024

@RyanCavanaugh So how would that work in this case? I basically want to have the input target class be of type HTMLElement or any sub class of it and then extend that target.

interface Aspect {
     feature(): void;
}

type Constructor<T extends HTMLElement> = { 
   new(...args: any[]): T;
   prototype: T
}

// MyDecorator here should be allowed on any class that extends from HTMLElement
function MyDecorator<T extends HTMLElement>() {
    // Side note, I'm returning Constructor<T & Aspect> here which should be the correct return type,
    // but I have to replace this with any unless I want to prevent myself from using static properties 
    // in a class that uses this - which I believe is this issue here:  https://github.com/microsoft/TypeScript/issues/4881
    // That's fine.  I'm not worried about that since I can just use interface merging to get around that problem.
    return function (target: Constructor<T>): Constructor<T & Aspect> {
        // @ts-expect-error - I have to put this tag here in order to get this to work.
        return class _MyDecorator extends target implements Aspect {
            public feature() {
                console.log('Some new thing that gets added to target via sub classing it as a new prototype in front');
            }
        }
    }
}

// MyComponent extends a div element (using is="my-component")
// This does work as expected as long as I ignore the TypeScript error with //@ts-ignore or //@ts-expect-error.
@MyDecorator()
class MyComponent extends HTMLDivElement {
}

If this isn't possible, or a duplicate of the myriad of issues defined in #4881, then we can close this and I'll just use the workarounds I've found.

from typescript.

typescript-bot avatar typescript-bot commented on May 1, 2024

This issue has been marked as "Question" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

from typescript.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.