type 类型
2025/4/16小于 1 分钟
type 类型
说明
对象属性字段的类型,声明类型是为了静态代码检查
案例
案例一
模型
class TestItem extends ModelBase {
@Column()
public msg?: string
}
class Test extends ModelBase {
@Column()
public a?: number
@Column()
public b?: string
@Column()
public c?: boolean
@Column({})
public d?: string[]
@Column({
model: () => TestItem,
default: () => [],
})
public e?: TestItem[]
@Column({
model: () => TestItem,
autowired: true,
})
public f?: TestItem
}
实例初始化
const test = new Test({
a: 1,
b: 'hhh',
c: true,
d: ['123', '456'],
e: [{ msg: '123' }, { msg: '456' }],
f: { msg: 'message' },
})
打印日志
console.log(test.a)
// 1
console.log(test.b)
// "hhh"
console.log(test.c)
// true
console.log(test.d)
// ["123", "456"]
console.log(test.e)
// [{ msg: "123" }, { msg: "456" }]
console.log(test.f)
// { msg: "message" }
注意事项
- 不需要传入,但是要正确的声明,通过 Reflect.getMetadata('design:type', target, property)自动读取
- 支持的数据类型Array、Object、Number、String、Boolean、Map、WeakMap、Set、WeakSet、Symbol、Function、File、Any
- Array、Object的特殊用法请参考
model