类型和值
/* 类型和值 */
class Animal_6201 {
name: string;
}
// 作为类型使用
let animal_6201: Animal_6201;
// 作为值使用
let animal_6202 = new Animal_6201();
interface People_6203 {
name: string;
}
// 作为类型使用
let people_6203: People_6203;
// 不能作为值使用
let people_6204 = new People_6203(); // 报错
作为类型使用 | 作为值使用 | |
---|---|---|
class | √ | √ |
enum | √ | √ |
interface | √ | |
type | √ | |
function | √ | |
var, let, const | √ |
答案
解析
类型与值使用表见下:
类型 | 作为类型使用 | 作为值使用 |
---|---|---|
class | ✓ | ✓ |
enum | ✓ | ✓ |
interface | ✓ | |
type | ✓ | |
function | ✓ | |
var,let,const | ✓ |
根据表可以看出:
- A - 类可以作为类型和值使用。正确。
- B - 枚举可以作为类型和值使用。正确。
- C - 接口可以作为类型使用,不能作为值使用。错误。
- D - 类型别名可以作为类型使用,不能作为值使用。错误。
接口合并
interface People_6205 {
name: string;
age: number;
}
interface People_6205 {
gender: 'male' | 'female'
}
let p: People_6205;
p.age // 同样拥有name, age, gender属性
答案
解析
接口合并就是把接口的所有成员放到一个同名的接口里。故答案选 C。