I want to write a generic function that takes as input an interface and a variable as sees if the names and data types of the variable match that of the typeface. Or do something similar. But I can't find anything. How would I do this?
Asked
Active
Viewed 93 times
0
-
Could you post some code that you've tried that maybe illustrates exactly what you're trying to do? – xdumaine Feb 21 '21 at 00:46
-
You might be looking for "User defined type guards" -> https://www.typescriptlang.org/docs/handbook/advanced-types.html – Evan Trimboli Feb 21 '21 at 00:47
-
duplication of [https://stackoverflow.com/questions/43909566/get-keys-of-a-typescript-interface-as-array-of-strings]? – Stefan Feb 21 '21 at 01:36
-
You cannot pass an `interface` as an argument to a function because typescript types do not exist at runtime. If you are dealing with a `class` that `implements` an `interface` then you can use `instanceof`. Otherwise, this cannot be handled in a generalized, generic way. – Linda Paiste Feb 21 '21 at 03:51