Using the typescript compiler, is it possible to check that the member types of an interface are one of a fixed set? For example, if i wanted to ensure they can all be serialized, I might only allow strings and numbers (I'm aware other types can be serialized), and want the compiler to complain if i used a member with a different type?
Asked
Active
Viewed 489 times
2
-
Are you asking about constraining all the properties of an interface, or individual ones? Maybe I'm misunderstanding, but i guess you are looking for a union type: `prop: string | number` ? – bugs Oct 11 '18 at 12:24
-
Possible duplicate of [Constraints on interface members in typescript generics](https://stackoverflow.com/questions/52730241/constraints-on-interface-members-in-typescript-generics) – Matt McCutchen Oct 11 '18 at 13:07
-
I'm trying to achieve the same thing. Did you ever find a solution richzilla? – Rengers Sep 04 '19 at 16:52