1

I'm quite new to Typescript and struggling with a, what seems to be, easy problem in Typescript (I come from C++, so generics/templates shouldn't be an issue to understand). I'm constantly struggling quite a lot with type checking in Typescript. I keep thinking I understand it, but then don't I when I need it. I've been searching, but a lot of methods don't seem to do what I want to do as I feel like it's to specific.

I have a generic interface with some (optional) properties. One of these properties is the type of the generic (code is shown below). Next to that I have a generic Angular Service with some methods. These methods use that interface in their argument list. Now, one of these functions have to check the if the argument type that is received in the argument list is exactly the same as the type of the generic type of the Angular Service.

I don't know if this all makes sense, but here's what I explained, but then in code:

interface MyInterface<T> {
    command: string;
    channel: string;
    data?: T;
};

interface MyClass {
   url: string;
   params?: string[]
};

@Injectable({...})
class MyService<T> implements MyServiceParent<T> {
    ...

    match(obj : MyInterface<T>): boolean {
       // check if obj is of type MyInterface<MyClass>
    }
};

The thing is, I may be able to check this on build time, but this check must happen during runtime as 'T' of MyInterface can be anything even if MyService's generic is MyClass. The compiler/builder can't check its type as the data that is passed into MyService::match will come from a post message (the app is in an iframe). This means that I can post a message with, for example, a string and Typescript will compile fine as the data isn't predefined. This is exactly why I need to check for the type during runtime, because this service can only handle that specific type (in this case: MyInterface<MyClass>).

I hope it's clear. Please ask if something isn't clear.

Thanks for your time!

UPDATE

A code playground for this problem can be found in the following Typescript playground

M Zeinstra
  • 1,931
  • 4
  • 17
  • 46
  • 2
    Does this answer your question? [How to check the object type on runtime in TypeScript?](https://stackoverflow.com/questions/44078205/how-to-check-the-object-type-on-runtime-in-typescript) – Max Mar 02 '20 at 20:49
  • @Max Does that mean that I have to create a function myself to check it's type? Because I was hoping on a method that handles this by itself. – M Zeinstra Mar 02 '20 at 21:07
  • It seems like I can use it to do what I want, but I really had hoped (and still do!) to have a build-in functionality for it. – M Zeinstra Mar 02 '20 at 21:47
  • `MyClass` doesn't seem to be a type in your example code, and the `MyClass` value is not specified enough for someone to suggest a particular solution. You will undoubtedly need to write your own runtime test for any particular type you care about; you can annotate this test as a type guard function so the compiler understands what it does. The answer to the question this duplicates shows how to do this. If you need something different, please edit the code above to constitute a [mcve] that demonstrates exactly what you need and why a user-defined type guard doesn't work for you. Good luck! – jcalz Mar 03 '20 at 01:53
  • @jcalz Thanks :) I've updated my question so it's reproducible. I hope anyone got the solution I want. In the meanwhile, I'll just use the answer that has been shared by Max. I'll make that an answer if I didn't find another solution :) – M Zeinstra Mar 03 '20 at 06:23
  • Thanks; yeah this is essentially a duplicate of the other question. It doesn't look like you're looking for anything substantially different. I'm closing this in favor of that one [and this one](https://stackoverflow.com/a/59483318/2887218). Good luck! – jcalz Mar 03 '20 at 16:05

1 Answers1

0

Typescript has a property called kind. Documentation

Its less expensive as typeof.

interface MyInterface {
   kind: "myObj";
}

function area(s: Obj) {
   switch (s.kind) {
       case "myObj": return ;
       default :
          throw NotCorrectObjException(Obj.kind);
   }
}
Max
  • 120
  • 1
  • 9
  • I don't see how this answer applies; the code here doesn't seem obviously related to the question code with the exception of the name `MyInterface`. You seem to be suggesting that the question use a [discriminated union](http://www.typescriptlang.org/docs/handbook/advanced-types.html#discriminated-unions) to tell types apart, but this only works if you need to distinguish among a finite list of types whose definitions you can alter to include a discriminant property. If this is or is not what you're trying to convey it would help if you explain it more explicitly. – jcalz Mar 03 '20 at 01:56