Typescript: Get type of an instance's generic

typescript check type
typescript check if object is type of interface
typescript get type of property
typescript keyof
typescript dictionary type
typescript object type
typescript index type
typescript omit multiple

The problem:

Say I have a interface from a third party library that uses generics

interface SomeInterface<T> {
    ...
}

And in my code I have an instance that implements that interface

const someInstance; // type signature: SomeInterface<string>

Given this instance, how would I access the type of the generic type parameter T for that instance (in this example, how would I be able to extract the string type from someInstance)? I don't need it for runtime, I just need it so that I can define what type to expect as an argument in a function:

function someFunction(someArg: ???) {...}

Basically I want to be able to do this, which doesn't work:

function someFunction(someArg: typeof T in someInstance) {...}

Specific use case:

My specific use case here is that I'm using the redux-act and redux-sagas packages. Redux-act provides an action creator factory which produces an action creator with a type signature of ActionCreator<P, M>

// someActionCreator has type signature of ActionCreator<string, void>
const someActionCreator = createAction<string, number>(...); 

When this action creator is called via someActionCreator(payload: P, metadata: M), it produces an Action<P, M>.

// someAction has a type signature of Action<string, number>
const someAction = someActionCreator("foo", 1);

In redux sagas, I have access to the action creator instances (ie someActionCreator), where P and M have defined types, but I don't have access to the actions themselves. However, the handler functions expect the action as the argument, such as

function* someEffectHandler(action: Action<string, void>) {...}

Since Typescript knows what the types of P and M are on someActionCreator, I want to be able to access them inside the type declaration of someEffectHandler. What I'm trying to avoid is having to write tons of boilerplate for every single action, when the action creator should be able to give me the typed parameters.

//Trying to avoid this
type SomeActionPayload = string;
type SomeActionMetadata = number;
export type SomeAction = Action<SomeActionPayload, SomeActionMetadata>;
export const someActionCreator = createAction<SomeActionPayload, SomeActionMetadata>(...);
type GetInnerType<S> = S extends SomeInterface<infer T> ? T : never

usage:

type InnerType = GetInnerType<typeof someInstance>

JavaScrpt typeOf: TypeScript - get types from data using typeof, Some snippets how to create types based on initial data. Tagged with typescript, javascript, types. This operator used to get a string specifying the type of variable. This will very helpful for developers to know the type of the object at runtime. typeof operator syntax. typeof {operand} parameter operand is supplied either as the primitive data type of custom object. possible operand values are variables, object variables, and strings.

You can play around a bit and you can get the generic arguments out from a generic type.

// Helper method, this will declare it returns a tuple of the two generic argument types, but at runtime it will just return an empty array. Used only to help with type inference.

function inferHelper<P, M>(creator: (p: P, m:M) => Action<P,M>) : [P, M]{
    return <any>[];
}
// Dummy variable, will be typed as [string, number] in your example, but will not actually hold anything at runtime
let helper = inferHelper(someActionCreator); // Dummy variable should not be used
// we get the type of the first tuple element, which we can then use 
type firstArg = typeof helper[0];
// and the second
type secondArg = typeof helper[1];

I am not 100% sure that this solution achieves your stated goal of having less boilerplate code but it does extract the generic arguments. One advantage is perhaps that if you refactor the original action you don't have to change any types, all types will be inferred correctly wherever you use firstArg and sec

Checking the type of an object in Typescript: the type guards, In the example below, we have two interfaces, Animal and Person: Animal or Person ? Both have a name property… If we want to print the name,  As you know, we can create an object of any function using the new keyword. Sometimes you want to know the type of an object to perform some action on it. Use the typeof operator to get the type of an object or variable in JavaScript.

You can make the types work like below. Not sure if this is what you're looking for.

// define the function
function someFunction<U extends SomeInterface<T>>(someArg: T) {...}

// call the function
someFunction<typeof someInstance>(someArg) {
    // someArg will have type string
}

Get an object's class name at runtime, Is it possible to get an object's class/type name at runtime using TypeScript? class MyClass{} var instance = new  tl;dr. When checking for primitive types in TypeScript , typeof variable === “string” or typeof variable === “number” should do the job.. When checking for HTMLElement or HTMLDivElement type of objects, variable instanceof HTMLElement or variable instanceof HTMLDivElement type of check should be the right one.

A Look at the instanceof Type Guard in TypeScript ← Alligator.io, An intro to the instanceof operator in TypeScript used as a type guard to get In the code example above, we have two classes, Chef and Driver which both init which acts differently depending on the instance passed into it. @DanielRosenwasser, could we have any of that love in TypeScript? Even if not, TypeScript loves you. 😄 You have the exact same situation in C#. Actually, @AlicanC, you can specify a new Constraint in C#. The difference is that in C#, there is a straightforward correspondence between types and their constructors. We're not really blessed with

How to get the types you want with TypeScript type guards, The second built-in type guard, instanceof , is a bit more interesting. You can use it to check if a variable is an instance of a given class. I wonder  An intersection type combines multiple types into one. This allows you to add together existing types to get a single type that has all the features you need. For example, Person & Serializable & Loggable is a Person and Serializable and Loggable. That means an object of this type will have all members of all three types.

Advanced TypeScript Concepts: Classes and Types, A TypeScript interface containing all the instance methods and properties of the class; and We'd have to write the entire type all over again. TypeScript is a structural type system. When we compare two different types, regardless of where they came from, if the types of all members are compatible, then we say the types themselves are compatible. However, when comparing types that have private and protected members, we treat these types differently.

Comments
  • Not always possible, but you can drill down to the property with type T and do typeof that property.
  • @unional Unfortunately, because the instance doesn't have a value for T yet (just what type it expects it to be), there is no property on the instance to call typeof on.
  • Is it possible to generic type of any type? Like S extends any<infer T>
  • @fregante resolving T in that case is not useful because you don't know what it can be used for. You don't know if it is the type of - for example - a property or return or input so you can't do anything with it.