I have the following interface as an example:
interface Account {
email: string;
enabled: boolean;
}
I want to create a method that will return default values for the fields in this interface. This is what I have so far:
function defaultValue(propName:string) {
if (propName === 'email') return '';
if (propName === 'enabled') return true;
}
While this method works fine, it's not foolproof. If any property within Account
changes, the defaultValue()
method may still compile correctly even though it's now incorrect.
Is there a way to specify that propName
should match the type of a property name in Account
? Or is there another effective pattern I can implement to ensure type checking in this scenario?