Comments (5)
This is also possibly related to #4628. Not in the sense that my issue is affected by static inheritance, but because if this issue were resolved by the addition of something like the extends private
mechanism described here, that could also be used to resolve 4628, by allowing decoupling of inherited members vs exposed members.
from typescript.
It occurs to me that one radical way to solve this problem would be to add some syntax that allows you to suppress creating the interface portion of the class altogether. Then you could simply define the interface separately with whatever subset of the actual functionality you want to expose, and then the ES6 class definition just gets matched against that interface, rather than generating the interface itself.
...Huh. This actually feels like a useful and actionable feature. You could rewrite the original example like this:
// This line is the only definition of the MyTuple<T> type
export interface MyTuple<T> extends ReadonlyArray<T> { }
// A private class is one whose types are not visible to the outside. It has signature { } or possibly object.
export private class MyTuple<T> extends Array<T>
// This one implements the MyTuple<T> interface to ensure that it matches its public API
implements MyTuple<T> {
constructor(...args: readonly T[]) {
super();
this.push(...args);
Object.freeze(this);
}
}
What do you think?
from typescript.
@dmchurch You can use a class expression to make it "private".
// This line is the only definition of the MyTuple<T> type
export interface MyTuple<T> extends ReadonlyArray<T> { }
// A private class is one whose types are not visible to the outside. It has signature { } or possibly object.
export const MyTuple = class<T> extends Array<T>
// This one implements the MyTuple<T> interface to ensure that it matches its public API
implements MyTuple<T> {
constructor(...args: readonly T[]) {
super();
this.push(...args);
Object.freeze(this);
}
}
Note that you MUST NOT use the name MyTuple
for the class expression. Use any other name or anonymous instead, otherwise the interface MyTuple<T>
declared in the outer scope will be shadowed.
from typescript.
@whzx5byb Yes, I've used that workaround before, and it's not applicable in this case. I need the emit to contain an ECMAScript top-level class export, which has slightly different semantics than an anonymous class assigned to a const (or a let, or a var, etc etc). If you have any suggestions that actually fit my needs, I'm all ears.
from typescript.
I was looking for something similar, I wanted my subclass to be a readonly array. After playing with all the possibilities I managed frankenstein this together from the bits an pieces on StackOverflow and this repos issues.
function ReadonlyArrayCtor(): { new <T>(...items: readonly T[]): ReadonlyArray<T> } {
return Array as any;
}
export class MyTuple<T> extends ReadonlyArrayCtor()<T> {
constructor(...args: readonly T[]) {
super(...args);
Object.freeze(this);
}
}
let vec = new MyTuple(1, 2);
vec[1] = 100; // error
Kinda works. But couldn't make it an actual tuple though. Even casting the constructor factory to readonly [number, number]
did not limit the subclass to only 2 indexes. I guess extends
broadens the type to have "at least two".
function ReadonlyArrayCtor(): { new (x: number, y: number): readonly [number, number] } {
return Array as any;
}
export class MyTuple<T> extends ReadonlyArrayCtor() {
constructor(x: number, y: number) {
super(x, y);
Object.freeze(this);
}
}
let vec = new MyTuple(1, 2);
let x = vec[0]
let y = vec[1]
let z = vec[2] // no error
vec[1] = 100; // error
from typescript.
Related Issues (20)
- HTMLFormElement disallows symbol keys HOT 5
- TypeScript language service cannot find subclass references/implementation of mixin methods
- when using ts.getJSDocTags, the value of @type is not returned. Is there any solution? HOT 2
- Compiler allows narrower method signature than implemented interface HOT 7
- Allow overload signatures to have different access levels HOT 3
- Watcher recursively watches irrelevant directories HOT 11
- Auto-closing of tags within curly braces `{}` does not work when parent element is same tag in JSX HOT 1
- Undocumented breaking change in 5.5.0-beta for reference directives HOT 3
- 5.5.0 regression - importHelpers do not work with moduleResolution: bundler HOT 1
- 5.5.0 inheriting outDir: ${configDir} does not automatically exclude it from compilation HOT 3
- isolatedDeclarations wrongly enabled in VSCode HOT 3
- ESNext Set methods HOT 1
- isolatedDeclarations should not have warning for functions that have no return statements HOT 4
- Trivia ownership documentation seems to be incorrect
- `--isolatedDeclarations` allows generator functions HOT 1
- Allow `--noCheck` on the CLI with top-level `--build`
- Union in template literal simplifying unexpectedly HOT 2
- Default import from an ESM package into a CommonJS project with esModuleInterop is faulty
- Watch cannot handle large amount of file changes HOT 1
- generator next type should be inferred as union (instead of intersection) of yields' types OR just unknown
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.