How to determine type of variable in typescript, array of numbers or array of strings?

typescript array of objects
typescript typeof
typescript array of strings
typescript array of arrays
typescript array of objects interface
typescript union types
typescript type alias
typescript array of tuples

I have a variable like

const data = [];

In some places in my App, data looks like string array or number array

// data [1, 2, 3]
// data ["1", "2", "3"]

How can I get the type of this variable - type string array or number array ?

@Tomas Vancoillie has the right idea.

You can declare them with the : operator.

You can also infer the type with Array(), for example:

let myStringArray = Array<string>();

Handbook - Basic Types, As in other languages, we use the type string to refer to these textual datatypes. Tuple types allow you to express an array with a fixed number of elements We may need to describe the type of variables that we do not know when we are� const x: number = 1 const y: number = x + 1; In TypeScript, there is the BigInt type to store numbers that are beyond the safe integer range. A BigInt number can be created by adding an n character to the end of a number. With BigInt, we can make calculations that have results beyond the safe range of normal numbers.

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

How To Check Data Types in JavaScript using typeof, Both typeof null and typeof an array return "object" in a potentially undefined; object (meaning null ); boolean; number; bigint; string type in JavaScript, and undeclared variables are also considered to be undefined. Defining array with multiple types in TypeScript. Use a union type (string|number) [] demo: const foo: (string|number) [] = [ 1, "message" ]; I have an array of the form: [ 1, "message" ]. If you are sure that there are always only two elements [number, string] then you can declare it as a tuple: const foo: [number, string] = [ 1, "message" ];

If you only need to get the type of the elements in the, since they are either string or number, you can just get the type of the first element, since all other elements have the same type:

typeof data[0] // string or number

JavaScrpt typeOf: TypeScript - get types from data using typeof, A great way to make sure the values you set to your variables type Data = typeof data; // type Data = { // value: number; // text: string; // } Since TypeScript 3.4 you can do the following if you have an array of strings (notice the as const ): something i've never seen in typescript, and can't find on the doc site. In the above example, we declared one variable with let keyword of type string and after that, we have given square brackets which shows us it is a type of array. After that, it is equal (=) sign and we gave the values in square brackets separated by commas. Syntax #2: Now, in this type, we are explicitly writing array.

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)[] = [];

Data Structures: Objects and Arrays :: Eloquent JavaScript, Both string and array values contain, in addition to the length property, a number of Values of the type object are arbitrary collections of properties. To compute the measure of correlation between two Boolean variables, we can use the phi� An array in TypeScript can contain elements of different data types using a generic array type syntax, as shown below. Example: Multi Type Array. let values: (string | number) [] = ['Apple', 2, 'Orange', 3, 4, 'Banana']; // or let values: Array<string | number> = ['Apple', 2, 'Orange', 3, 4, 'Banana'];

Learn TypeScript Data Types – From Zero to Hero, Check the screenshot below (this is inside an Angular app):. typescript and Strings in TypeScript work the same way as they do in JavaScript. Variables of Variables of data type number are declared like so: const myNum: in JavaScript. Variables of data type array are declared two separate ways :. Below array is the input and expecting respective O/P. 22,6,-1]// This variable will automatically be assigned a number[] array type. let arr: number [] = []; let arr: Array number > = []; Normally, TypeScript can infer the type of a variable if there is an assignment. Lodash is available in a variety of builds & module formats.

Indexed collections, JavaScript does not have an explicit array data type. It has a property for determining the array length and other properties for use with regular expressions. In addition to a newly defined variable as shown above, arrays can also let arr = [42] // Creates an array with only one element: // the number 42. Type checking. Our problem is classic in Typescript: an object type is undifferentiated, and we would like to differentiate the different cases.

JavaScript data types and data structures, Variables in JavaScript are not directly associated with any particular value let foo = 42; // foo is now a number foo = 'bar'; // foo is now a string foo All types except objects define immutable values (that is, values which can't be changed). For example, indexOf (searching a value in the array) or push� Tuple types in TypeScript express an array where the type of certain elements is known. orderedItems is a computed that goes through items in order and creates an observable for position. array will create a faux-array (array-like object) instead of a real array.

Comments
  • Is it possible to have both numbers and strings in an array ?
  • It seems like you have different data in it at different points. Settle on just one - whatever makes sense for your use-case, then give the variable that type and then ensure you only have one type going into it. For example, if you declare it to be number[] then convert all input to numeric before adding into the array.
  • @RomiHalasz Yes, no real problem with that ["hello", 42, true] is valid.
  • @RomiHalasz No only number or only string.
  • @0xD34F Array of numbers or array of strings - string[] | number[]
  • if specify Array<string | number> Will it be (1) only strings and only numbers? Or (2)strings and numbers?
  • @0xD34F strings and numbers.
  • Array is of Type T, which means , it will be strings and numbers.
  • @Paul How to set type T ?
  • @0xD34F T means generic, it accepts everything. If you want to use anything, use the type <any>
  • This is what I need. But how to specify only lines or only numbers together?
  • @0xD34F look here if you want to use multiple types stackoverflow.com/a/29382420/1934484