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

Inner types #46191

Closed
5 tasks done
FilipeBeck opened this issue Oct 4, 2021 · 4 comments
Closed
5 tasks done

Inner types #46191

FilipeBeck opened this issue Oct 4, 2021 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@FilipeBeck
Copy link

Suggestion

πŸ” Search Terms

  • inner types
  • nested types

βœ… Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

A kind of "syntax sugar" to merge declarations. It is equivalent to using namespace. Some possibilities:

/**
 * Classes and interfaces 
 */

class Listing <T> {
    inner interface Pagination {
        page: number
        totalPages: number
        records: T[]
    }
    // `Pagination` is implicitly `Pagination<T>` 
    constructor(private pagination: Pagination) {}
}

const aPagination: Listing<string>.Pagination = {
    // ...
}

/**
 * Functions.
 */

function fetch(method: fetch.Method) { // Maybe, just `fetch(method: Method)` is enough
    inner type Method = 'GET' | 'PUT' | 'POST'

    const request = // ...
}

const aMethod: fetch.Method = 'GET'


/**
 * Types.
 */
type Payload = Payload.User | Payload.Address { // Maybe, just `Payload = User | Address` is enough
    inner interface User {
        // ...
    }

    inner interface Address {
        // ...
    }
}

const aPayload: Payload = Payload.Address
  • Maybe inner is not the better keyword name.
  • Maybe a access level for classes (public, protected, private).
@jcalz
Copy link
Contributor

jcalz commented Oct 4, 2021

Seems like #7061

@tjjfvi
Copy link
Contributor

tjjfvi commented Oct 4, 2021

Also #17588

@MartinJohns
Copy link
Contributor

MartinJohns commented Oct 4, 2021

Also somewhat related: #41470.


I find the idea of "local" types declared within the implementation body to leak outside extremely unpleasant and disturbing. This would also require additional new syntax for declaration files, which was not considered at all in this issue. I hardly see any benefit and a lot of complexity.

And unfortunately the issue template for feature requests was not filled out.

@andrewbranch andrewbranch added the Duplicate An existing issue was already created label Oct 6, 2021
@typescript-bot
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

6 participants