Comments (3)
Subclassing doesn't guarantee subtyping. Anything declared in the derived class might illegally override something in the base class
from typescript.
@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.
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)
- Error: Debug Failure. False expression: Expected the specifier to be a default export HOT 1
- type assertions affect the type narrowing of the subsequent code HOT 6
- Missing interface definition for Highlight API HOT 1
- Improve new issue template selection - reorder and handle "this is crash" check better HOT 3
- Union type breaks type safety of dynamic object keys HOT 2
- Asserting changes type of original variable inline (the change reverts in subsequent lines) HOT 10
- "declare class" in a global is not visible in external files HOT 7
- Mapped type reported as incompatible when passed through another mapped type (5.4 regression) HOT 1
- Shorten error spans for missing key errors reported on object literals HOT 3
- Array prototype extensions fail circularly with `...args: T[]`, but not `args: T[]` HOT 6
- Inconsistent behaviour of es6 and esnext property initializers when using experimental property decorators HOT 3
- Spread operator doesn't remove readonly from object properties HOT 3
- Add option to throw error when `!` (NonNull expression) is used on a non-nullish expression HOT 2
- Inconsistent Type Narrowing with `never` Return Type Between Arrow and Traditional Functions HOT 2
- Class infers wrong generic parameter since e1874f3 HOT 2
- Promise returned from sync `dispose` method should not be awaited when disposing an asyc-disposable HOT 1
- Import ellision emit bug: usage of imports only in keys of interfaces does not result in those imports being elided as type-only
- `noUncheckedIndexedAccess` does not narrow properly when iterating with `for...in` HOT 4
- Omit private symbols from type HOT 6
- TS5055: Typescript includes files in "dist" folder for compilation when `rootDir` is not `./` but `./src` even after explicitly adding "dist" to `exclude` HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from typescript.