Typescript check if property in object in typesafe way

You don’t get an error because you use a string to check if the property exists.

You will get the error this way:

interface Obj{
   a: any;
}

const obj: Obj = { a: "test" };

if (obj.b)          // this is not allowed
if ("b" in obj)     // no error because you use string

If you want type checking to work for string properties you could add index signatures using this example

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)