在我的代码中看到已解析的类型别名将非常有用,尤其是在使用promises或streams时。更详细地说:
type FileName = string;
function a(x: FileName): string {
return x + '!';
}
a(2); // Argument of type 'number' is not assignable to parameter of type 'string'.那么为什么不使用... parameter of type 'FileName'.呢?在做DDD的时候会有很大的帮助。
现在,作为更真实的例子,
stream.of(...).map(service.fn1).flatMap(service.fn2).map(x => x/*?*/)
在某种程度上,我真的很怀念看到这个x到底是什么。
那么,有没有办法让编译器使用别名呢?
发布于 2016-11-14 18:25:01
因此,在我的例子中,这是一个不知道术语的问题。TypeScript具有结构化的类型系统。这意味着string =字符串,而不考虑别名。然而,有一些方法可以克服这一点。这是阅读材料:
https://basarat.gitbooks.io/typescript/content/docs/tips/nominalTyping.html
https://stackoverflow.com/questions/40585197
复制相似问题