0

I'm trying to ask by the type or class required to add an item in an Array using typescript, I tried this.

I wan't to build a loop to build buttons to add an element to arrays attributes whose extends from a parent common class example, GenericItem

Class Item extends GenericItem

End

The Main Class

Class mainClass

   items: Array<Item>(); 
   ...
   // Many of arrays 
End

Iterating the arrays and create buttons

// Loop each of  GenericItem Arrays

let add_btn = document.createElement('button');
add_btn.onclick = (e) => { 
                    // Add an object item to array using the correct class
                }
// end loop
1
  • 2
    This doesn't appear to be TypeScript code? Commented Jul 26, 2016 at 20:49

1 Answer 1

1

Add an object item to array using the correct class

You cannot access the generic parameters in variable declaration space. They only exist in the type declaration space. This is because TypeScript types are only there at compile time and purely for compile time checking.

More

Declaration spaces : https://basarat.gitbooks.io/typescript/content/docs/project/declarationspaces.html

Why TypeScript (why types don't exist at runtime): https://basarat.gitbooks.io/typescript/content/docs/why-typescript.html

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.