Yes you can type your variables with a single type
// type data as an array of strings
const data: string[] = [];
// type data as an array of numbers
const data: number[] = [];
// type data as an array of objects
const data: object[] = [];
If you want to use a mix of types in your array, you can type it as any. But this may not be a good practise.
const data: any[] = [];
For more information about type in typescript, look here: Basic type documentation
For defining array with multiple types, look here: https://stackoverflow.com/a/29382420/1934484
Answer from Tomas Vancoillie on Stack OverflowVideos
Yes you can type your variables with a single type
// type data as an array of strings
const data: string[] = [];
// type data as an array of numbers
const data: number[] = [];
// type data as an array of objects
const data: object[] = [];
If you want to use a mix of types in your array, you can type it as any. But this may not be a good practise.
const data: any[] = [];
For more information about type in typescript, look here: Basic type documentation
For defining array with multiple types, look here: https://stackoverflow.com/a/29382420/1934484
You can define your array like this the typescript compiler will only allow strings
const array: Array<string> = [];
or if you want to have strings and numbers define it like this
const array: Array<string | number> = [];
You can also define it this way
const array: string[] = [];
const array: (string | number)[] = [];
var StronglyTypedArray=function(){
this.values=[];
this.push=function(value){
if(value===0||parseInt(value)>0) this.values.push(value);
else return;//throw exception
};
this.get=function(index){
return this.values[index]
}
}
EDITS: use this as follows
var numbers=new StronglyTypedArray();
numbers.push(0);
numbers.push(2);
numbers.push(4);
numbers.push(6);
numbers.push(8);
alert(numbers.get(3)); //alerts 6
Array of specific type in typescript
export class RegisterFormComponent
{
genders = new Array<GenderType>();
loadGenders()
{
this.genders.push({name: "Male",isoCode: 1});
this.genders.push({name: "FeMale",isoCode: 2});
}
}
type GenderType = { name: string, isoCode: number }; // Specified format