string类型为什么是Object

来源:11-31 【元数据操作】 理解 reflect-metadata 元数据操作重载方法和其他方法 2

crazyones110

2021-10-07

class Test {
  @Reflect.metadata("aa", "bb")
  name = "xxx";
}

Reflect.getMetadataKeys(Test.prototype, "name").forEach(metadataKey => {
  console.log(metadataKey, Reflect.getMetadata(metadataKey, Test.prototype, "name"));
})
// 打印出design:type [Function: Object]
class Test {
  @Reflect.metadata("aa", "bb")
  name: string = "xxx";
}

Reflect.getMetadataKeys(Test.prototype, "name").forEach(metadataKey => {
  console.log(metadataKey, Reflect.getMetadata(metadataKey, Test.prototype, "name"));
})
// 打印出design:type [Function: String]

仅仅是显式指明类型是string, design:type的类型就正确了, 这是为什么

写回答

1回答

keviny79

2021-10-07

是的, 当你不加具体类型时,比如: name=["abc","cde"]    design:type 输出的结果都是object , 元数据 design:type认为  name:string="abc"  name="abc" 还是有区别的 ,   name="abc" 后面的"abc" 可以改成任何类型的值 ,所以可以被design:type输出为object   ,但是name:string="abc"  后面只能是字符串

1
1
crazyones110
非常感谢!
2021-10-07
共1条回复

晋级TypeScript高手,成为抢手的前端开发人才

轻松驾驭 TypeScript 高级用法, 突破前端成长瓶颈

871 学习 · 425 问题

查看课程