给定一个函数foo,在本例中,类型参数T被正确地推断为string:
declare function foo<T>(callback: (bar: T) => void): void
// foo<string>(callback: (bar: string) => void): void
// ---> T is inferred string here
foo((bar: string) => { })但是,下面的示例显示要推断为unknown的T。所以我的问题是:为什么类型不能通过嵌套在回调对象类型中的T来解析?
declare function foo2<T>(callback: (bar: { a: T }) => void): void
// foo2<unknown>(callback: (bar: { a: unknown; }) => void): void
// ---> T is inferred unknown here
foo2(({ a: string }) => { })发布于 2019-11-16 23:26:30
我想这就是你要找的
declare function foo<T>(callback: (bar: T) => void): void
foo((bar: string) => { })
declare function foo2<T>(callback: (bar: T) => void): void
foo2((a: { a: string }) => {} )https://stackoverflow.com/questions/58892116
复制相似问题