CNode

问一个关于 ts 的问题

问答
Llyf103104发布于8 年前最后回复8 年前4 回复3502 浏览0 收藏

WechatIMG22.png

WechatIMG23.png


  1. get 函数要求返回一个 promise 对象,而 return this.name,并不满足,为何编译通过?

  2. 直接 return 一个字符串,编译器报错,为什么 return this.name,却可以通过编译?(this.name 实际也是一个字符串)

查看回复

回复 (4)

Y
yviscool#1·8 年前

this.name 是个 any , 因为 this 运行时确定,你也可以手动指定 this

function get(this:string): Promise<string> {
    this.name =  'zjl';
    return this.name;
  } 
L
lyf103104#2·8 年前
引用 yviscoolthis.name 是个 any , 因为 this 运行时确定,你也可以手动指定 this

@yviscool

this.name 的类型是 any 可是 get 函数要求返回 promise 对象,这样也可以满足吗?

J
JsonSong89#3·8 年前
引用 lyf103104@yviscool this.name 的类型是 any 可是 get 函数要求返回 promise 对象,这样也可以满足吗?

@lyf103104 any全都能过,当然前提是你tsconfig里面允许.

我估计你是想这样写吧


async function get(this: string): Promise<string> {
    return ""
}

参与回复
登录后即可参与回复。登录