Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking type of a generic param in typescript

Tags:

typescript

I am working in a react app using typescript. One of the functions receives a generic type in one of the variables.

How can I check the type T of the variable? Is this possible?

MyFunction<T>(newValues: Array<T>) {
    if (typeof T === "string") {
        // do some stuff
    }
}
like image 628
Bonomi Avatar asked Feb 18 '19 16:02

Bonomi


People also ask

How do you get Typeof a variable in TypeScript?

Use the typeof operator to check the type of a variable in TypeScript, e.g. if (typeof myVar === 'string') {} . The typeof operator returns a string that indicates the type of the value and can be used as a type guard in TypeScript.

How TypeScript check types?

Using TypeScript type guards Checking a specific value's type at runtime is the primary function of type guards. This helps the TypeScript compiler, which then uses the information to become more predictive about the types. The above code snippet showcases the example of the type guard instanceof .

What is generic data type in TypeScript?

TypeScript Generics is a tool which provides a way to create reusable components. It creates a component that can work with a variety of data types rather than a single data type. It allows users to consume these components and use their own types.

How do you pass a generic type as parameter TypeScript?

Assigning Generic ParametersBy passing in the type with the <number> code, you are explicitly letting TypeScript know that you want the generic type parameter T of the identity function to be of type number . This will enforce the number type as the argument and the return value.


1 Answers

Well, kind of possible. You can check for the type of objects during runtime using typeof type guards like in your example as long as the type of T is one of JavaScript's primitive types (string, number, function, etc). Like so:

function printAll(strs: string | string[] | null) {
  if (typeof strs === "object") {
    for (const s of strs) {
      // Object is possibly 'null'.
      console.log(s);
    }
  } else if (typeof strs === "string") {
    console.log(strs);
  } else {
    // do nothing
  }
}

If T can be an object that you know how to identify, using type predicates would be a way to go. These are user defined functions that identify if an argument is of a specific type.

You can find more information on these two methods and a few more in the TypeScript Handbook, on the "Narrowing" chapter: https://www.typescriptlang.org/docs/handbook/2/narrowing.html

Keep in mind that you are checking for the types of the parameters, not the generic type T itself. Ideally, your function should work without knowing exactly what T is, and instead just using it as a placeholder for what T will be when the method is called.

You need to be careful, however, since type guards are used when trying to differentiate between two or more known types, not when determining the type of a generic.

Keep in mind that if you need to determine the type of a generic, you might not actually need a generic. If you know what are the possible types that a function could be called with, and those types matter, I'd suggest you look into union types.

It is not the intention of TypeScript to alter JavaScript at runtime, but instead to make it easier for tools and the developer to develop in JavaScript. All the magic of TypeScript should happen before the code is sent to the browser.

Hope that helps!

like image 157
Sammy I. Avatar answered Sep 18 '22 08:09

Sammy I.