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

Proposal: Using sub-types of an interface or class for typing #12481

Closed
renehamburger opened this issue Nov 24, 2016 · 2 comments
Closed

Proposal: Using sub-types of an interface or class for typing #12481

renehamburger opened this issue Nov 24, 2016 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@renehamburger
Copy link

There are many scenarios where complex type hierarchies (interfaces/classes) are needed, for example when typing the response of an API or the options object of a JavaScript library. But it can be quite cumbersome to define each segment/sub-type in such a hierarchy separately in order to use it as a type elsewhere.

It is already possible to use the type of any nested property of an instance for typing:

interface Response { 
    user: {
        id: string;
        name: string;
    };
    data: any;
}

const r: Response = null;

const user: typeof r.user = {
    id: '1',
    name: 'SomeUser'
};

But it would be convenient to do that without the need of an instance:

interface Response { 
    user: {
        id: string;
        name: string;
    };
    data: any;
}

const user: Response.user = {
    id: '1',
    name: 'SomeUser'
};

In this case it would be nice to have and save a lot of code if the interface has a very deep hierarchy. But it's even more important for type hierarchies declared in external definition files. See for example the Options interface defined for handsontable:

interface Options {
  ...
  // hooks
  afterAutofillApplyValues?: (startArea: any[], entireArea: any[]) => void;
  afterCellMetaReset?: () => void;
  afterChange?: (changes: any[], source: string) => void;
  afterChangesObserved?: () => void;
  afterColumnMove?: (startColumn: number, endColumn: number) => void;
  afterColumnResize?: (currentColumn: number, newSize: number, isDoubleClick: boolean) => void;
  afterColumnSort?: (column: number, order: boolean) => void;
   ...
}

None of these callbacks are defined separately, so it would be great to be able to use them directly with const cb: Options.afterAutofillApplyValues;.

(This may well have been proposed before, but I couldn't find anything in the current issue list.)

@yortus
Copy link
Contributor

yortus commented Nov 24, 2016

Looks like a duplicate of #4555, which was closed in favour of #4640 which was closed in favour of #6606.

Although the proposal in #6606 addresses this issue, the guidance from the team about what PR would be accepted does not cover it. Perhaps someone from the team can clarify whether anything has changed in terms of supporting this.

@yortus
Copy link
Contributor

yortus commented Nov 24, 2016

Note this comment in #6606 shows something similar that works in the current nightly.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Nov 28, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants