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: Expose collection element's type #3749

Closed
lazdmx opened this issue Jul 6, 2015 · 9 comments
Closed

Proposal: Expose collection element's type #3749

lazdmx opened this issue Jul 6, 2015 · 9 comments
Labels
Effort: Moderate Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual". Fixed A PR has been merged for this issue Help Wanted You can do this Suggestion An idea for TypeScript

Comments

@lazdmx
Copy link

lazdmx commented Jul 6, 2015

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
    }
}
@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Needs More Info The issue still hasn't been fully clarified labels Jul 6, 2015
@RyanCavanaugh
Copy link
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) { }
}

@lazdmx
Copy link
Author

lazdmx commented Jul 22, 2015

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

@RyanCavanaugh RyanCavanaugh added In Discussion Not yet reached consensus and removed Needs More Info The issue still hasn't been fully clarified labels Jul 22, 2015
@Andael
Copy link

Andael commented Aug 8, 2015

@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

@lazdmx
Copy link
Author

lazdmx commented Aug 8, 2015

@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}

@Andael
Copy link

Andael commented Aug 8, 2015

@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)

@RyanCavanaugh RyanCavanaugh added Help Wanted You can do this Effort: Moderate Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual". and removed In Discussion Not yet reached consensus labels Oct 5, 2015
@RyanCavanaugh RyanCavanaugh added this to the Community milestone Oct 5, 2015
@RyanCavanaugh
Copy link
Member

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

@yortus
Copy link
Contributor

yortus commented Jan 25, 2016

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.

@simonbuchan
Copy link

@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();
    }
}

@mhegazy
Copy link
Contributor

mhegazy commented Nov 20, 2017

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

@mhegazy mhegazy closed this as completed Nov 20, 2017
@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Nov 20, 2017
@mhegazy mhegazy modified the milestones: Community, TypeScript 2.6 Nov 20, 2017
@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
Effort: Moderate Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual". Fixed A PR has been merged for this issue Help Wanted You can do this Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

6 participants