Skip to content

Inner typesΒ #46191

Closed
Closed

Description

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).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions