Skip to content

Proposal: Expose collection element's type #3749

Closed

Description

I often needed (and I think others do) to get type of element of collection, especially when dealing with collections of complex type. As a workaround I introduce new dummy variable which is assigned with a value of collection element in order to get its type further through the typeof. Here is a sample with simple collection of type number[]

function test(a: number[]) {
    var dummy = a[0]

    return a.map(mapper)

    function mapper(v: typeof dummy) {
        // some code
    }
}

A bad things about this approach is need to introduce new dummy variable, and some unclarified declarations function mapper(v: typeof dummy)

So my proposal is to provide some kind of metadata (which is common for C++ STL, boost) in form of exported type properties like <...container type...>.ElementType.

Here is not a final syntax, but at least it looks more clear

function test(a: number[]) {
    return a.map(mapper)

    function mapper(v: (typeof a).ElementType) {
        // some code
    }
}

Activity

  1. RyanCavanaugh commented on Jul 6, 2015

    @RyanCavanaugh
    Member

    It'd be good to understand what kind of things are showing up in your code other than number that make introducing new syntax preferable to just writing function mapper(v: number).

    One alternative approach would be to allow indexing expressions with numeric literals in typeof positions. This would allow you to write:

    function fn(x: number[]) {
      type elem = typeof x[0];
      function foo(y: elem) { }
    }
  2. lazdmx commented on Jul 22, 2015

    @lazdmx
    Author

    Ryan Cavanaugh (@RyanCavanaugh) I think that allowing indexing expression with numeric literals in would be concise and good solution.

  3. Andael commented on Aug 8, 2015

    @Andael

    Ryan Cavanaugh (@RyanCavanaugh) While the original example has an array of a known type, the syntax would also be useful in the case where the array type is anonymous, e.g. from a .map() call:

    var data = [1, 2, 3].map(v => {
      return { raw: v, square: v * v };
    });
    type DataItem = typeof data[0]; // error
  4. lazdmx commented on Aug 8, 2015

    @lazdmx
    Author

    @ander-nz why? Since TS can derive type for data as {raw: number, square: number}[] then it will be able to derive a type for expression typeof data[0] as {raw: number, square: number}

  5. Andael commented on Aug 8, 2015

    @Andael

    lazutkin Sorry, my post was unclear, so I've updated it. (I meant that the type was unknown to the developer, so typeof would be useful)

  6. added
    Effort: ModerateRequires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual".
    and removed on Oct 5, 2015
  7. added this to the milestone on Oct 5, 2015
  8. RyanCavanaugh commented on Oct 5, 2015

    @RyanCavanaugh
    Member

    Approved + accepting PRs: allow typeof expr['str'] and typeof expr[42] in type positions.

  9. yortus commented on Jan 25, 2016

    @yortus
    Contributor

    I submitted a proposal + implementation that covers this: #6606

    However the proposal goes beyond what's suggested here so is more aligned with #4233, with this issue covered as a special case.

  10. simonbuchan commented on Aug 8, 2017

    @simonbuchan

    Ryan Cavanaugh (@RyanCavanaugh) looks like this can be closed now?

    function test(a: number[]) {
        return a.map(mapper)
    
        function mapper(v: typeof a[53]) { // or typeof a[number]
            return v.toExponential();
        }
    }
  11. mhegazy commented on Nov 20, 2017

    @mhegazy
    Contributor

    with indexed types, typeof a[number] should work. see it on the playground

  12. modified the milestones: , TypeScript 2.6 on Nov 20, 2017
  13. locked and limited conversation to collaborators on Jun 19, 2018
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

    Effort: ModerateRequires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual".FixedA PR has been merged for this issueHelp WantedYou can do thisSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions