xcatliu / typescript-tutorial

TypeScript 入门教程
https://ts.xcatliu.com
10.44k stars 1.33k forks source link

【基础】【联合类型】例子可能有点小问题 #176

Closed chokingwin closed 4 years ago

chokingwin commented 4 years ago

原文地址:访问联合类型的属性或方法

这个例子貌似有点小问题,返回值不能设置成 string 类型

function getString(something: string | number): string { return something.toString(); }

xcatliu commented 4 years ago

貌似没有问题呀 https://www.typescriptlang.org/play/index.html#code/GYVwdgxgLglg9mABAcwKZQMpQE4zMgCgGc4BbdACz2QC5EidrEAfRMEUgI1WwEo6GufIgDeAWABQiaYmzoQ2JCXJQq+AHRQ4WIYV4BuSQF9JkoA

chokingwin commented 4 years ago

貌似没有问题呀 https://www.typescriptlang.org/play/index.html#code/GYVwdgxgLglg9mABAcwKZQMpQE4zMgCgGc4BbdACz2QC5EidrEAfRMEUgI1WwEo6GufIgDeAWABQiaYmzoQ2JCXJQq+AHRQ4WIYV4BuSQF9JkoA

OK,没毛病。😅