Skip to content

Mangling interface bindings to indicate identifier type #61710

Closed

Description

@mda-jobs

Acknowledgement

  • I acknowledge that issues using this template may be closed without further explanation at the maintainer's discretion.

Comment

Regarding the following sample code:

import { Product } from "@shared/product.model";
export const engineers: Product[] = [...];

The engineers declaration uses the notation Product[], which is neither intuitive, nor logical, on the following counts:

  1. It imples that there is an interface definition: interface Product[] {...};
  2. The notation doesn't accurately reflect the element type of the engineers array, or what the [] applies to.
  3. Product[] looks like accessor syntax in which someone forgot the index.

The fix for this is to change the notation to:

export const engineers: [Product] = [...];

This correctly reflects the situation without mangling the interface binding and is closer to JS syntax.

Activity

  1. MartinJohns commented on May 16, 2025

    @MartinJohns
    Contributor

    [Product] is already working syntax and means a tuple with exactly one element.

    You can write Array<Product> instead, which is very clear if the other syntax confuses you.

    It imples that there is an interface definition: interface Product[] {...};

    It really doesn't, because [] are not valid identifier characters.

    The notation doesn't accurately reflect the element type of the engineers array, or what the [] applies to.

    I mean.. it does. The [] always applies to the type before. Honestly, if basic syntax (that is fairly common in a lot of languages) is so troublesome to you, you'll likely going to have a bad time. Better learn the syntax and get used to it.

  2. snarbles2 commented on May 16, 2025

    @snarbles2

    This sort of syntax is hardly unique to Typescript. It would look familiar to someone coming from one of many other typed languages.

  3. RyanCavanaugh commented on May 16, 2025

    @RyanCavanaugh
    Member

    We can't change the syntax fundamentals of a programming language a dozen years after its creation, sorry.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    UnactionableThere isn't something we can do with this issue

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions