为了账号安全,请及时绑定邮箱和手机立即绑定

typescript中rest参数如何进行类型检验

typescript中rest参数如何进行类型检验

慕粉1845456658 2020-05-02 21:29:56
代码如下:class Person {    name: string    age: number    constructor(name: string, age: number) {        this.name = name        this.age = age    }}function createInstance<T>(ctor: new (...params) => T, ...args): T {    return new ctor(...args)}const person = createInstance<Person>(Person, 123, 20) // should be error: 类型“123”的参数不能赋给类型“string”的参数console.log(person.name) // 123console.log(person.name.length) // undefined我的问题是:1、Person的constructor方法中,name参数应该是字符型;但在createInstance<Person>(Person, 123, 20) 中传入的123是number类型,但是typescript没有提示类型错误,导致person.name.length为undefined。2、如何给rest参数...params进行类型校验?
查看完整描述

目前暂无任何回答

  • 0 回答
  • 0 关注
  • 796 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号