Skip to content

Question: How to get the the generic type of Array? #262

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

Closed
LiaoPeng opened this issue Sep 12, 2018 · 9 comments
Closed

Question: How to get the the generic type of Array? #262

LiaoPeng opened this issue Sep 12, 2018 · 9 comments
Labels

Comments

@LiaoPeng
Copy link
Contributor

The requirement is to create obj by generic type.

class A { }
function createObj<T>(): T {
    var isArr = isArray<T>();
    if (isArr) {
        let arr = {} as T;
        // Is there a way to create an instance of A and push the instance to the `arr`
       return arr;
    }
    // other logic
}
createObj<Array<A>>();
@dcodeIO
Copy link
Member

dcodeIO commented Sep 12, 2018

<T>{] should work to create an instance using the default constructor (the one without arguments). Doesn't it?

Possibly related: #258

@LiaoPeng
Copy link
Contributor Author

@dcodeIO Thanks for you reply.
The above code, the generic T represented Array<A>. Is there a way to create an instance of A in function createObj<T>():T. I want to create an array with some elements.

@dcodeIO
Copy link
Member

dcodeIO commented Sep 18, 2018

At this point I think that there must be a constructor with zero arguments for it to work, that is until this TODO has been addressed.

@LiaoPeng
Copy link
Contributor Author

LiaoPeng commented Sep 19, 2018

@dcodeIO Thanks for you reply.
// TODO: if the constructor requires parameters, check whether these are given as part of the object literal and use them to call the ctor while not generating a store.

  1. If the constructor requires parameters, the {} not providers parameters. Not generating a store, so creating an instance without using constructor. It maybe lead to create an unsafe instance if the constructor has some restrictions. eg:
class A{
      year: i32;
      constructor(a: i32) {
              if ( a < 1900) {
                   this.year = 1900;
              }
             this.year = a;
      }
}

In that case calling the ctor with default parameter value (basic type value is 0, object type is null or {}) maybe a choice.

@MaxGraey
Copy link
Member

MaxGraey commented Feb 7, 2019

@dcodeIO Could we close this for now?

@dcodeIO
Copy link
Member

dcodeIO commented Feb 7, 2019

Yeah, seems this is pretty similar to #262, which is our tracking issue now.

@dcodeIO dcodeIO closed this as completed Feb 7, 2019
@trusktr
Copy link
Member

trusktr commented Jun 13, 2019

#262 is this very issue. Did you mean #258?

@trusktr
Copy link
Member

trusktr commented Jun 13, 2019

Just my sentiment here, but stuff like isArray<T>() is a bit awkward and straying away from the TypeScript language. My IDE treats it as an error, and plus isn't portable.

Is there another way to allow the same thing in AS, while being compatible with TypeScript?

@MaxGraey
Copy link
Member

isArray(expr) is portable, isArray<T>() - isn't. Also you could use Array.isArray(arr)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants