How do you check in typescript if variable is an instance of interface?
Since types are not available at runtime typescript allows you to define type checks that you could use at runtime like:
interface Keyboard {
keys: number;
}
function instanceOfKeyboard(object: any): object is Keyboard {
return 'keys' in object;
}
const kb = { keys: 5 }
if (instanceOfKeyboard(kb)) {
alert(kb.member);
}
15+ Years strategising and delivering growth, engineering, customer value and more. I have served as a CTO to multiple organizations, including Vamstar, AtruHelp, Billaway, SuperSehat, and more.
If you're a founder or CEO eager to move faster and seek tailored strategies for your unique challenges, don't navigate this journey alone. Reach out to me. Together, we can dissect, refine, and optimize your enterprise's trajectory to withstand the tests of time and innovation. Let's make your vision not just a goal, but an impending reality.
I have limited open hours. Book Appointment
If you're a founder or CEO eager to move faster and seek tailored strategies for your unique challenges, don't navigate this journey alone. Reach out to me. Together, we can dissect, refine, and optimize your enterprise's trajectory to withstand the tests of time and innovation. Let's make your vision not just a goal, but an impending reality.
I have limited open hours. Book Appointment