【发布时间】:2019-06-17 07:18:24
【问题描述】:
interface Students {
[key:string]: (Student | undefined);
}
interface Student {
age: string;
}
function something (students:Students, student_name:string) {
let student:Student;
if (typeof students[student_name] !== 'undefined')
student = students[student_name]; // Type 'Student | undefined' is not assignable to type 'Student'.
else
return;
}
我经常遇到这种情况。当然,我可以使用作为学生。但是,我想知道是否有更好的方法。
【问题讨论】:
标签: typescript