Giter Club home page Giter Club logo

Comments (4)

dmchurch avatar dmchurch commented on April 28, 2024

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.

dmchurch avatar dmchurch commented on April 28, 2024

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.

whzx5byb avatar whzx5byb commented on April 28, 2024

@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.

dmchurch avatar dmchurch commented on April 28, 2024

@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.

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.