class StorageUnit {
records: Record<string, string> = {};
getEntry(key: string, defaultValue?: string): string | undefined {
return this.records[key] ?? defaultValue;
}
}
const unit = new StorageUnit();
const entry1 = unit.getEntry("test");
const entry2 = unit.getEntry("test", "def");
- The variable
entry1
is of typestring | undefined
, which is expected behavior. - However,
entry2
also has the same type asentry1
. How can we modify the signature ofgetEntry()
so that when a default value is provided, there is noundefined
? For example,entry2
should be of typestring
only.