TypeScript Map类型中的回调函数隐式具有'any'类型,

jrcvhitl  于 9个月前  发布在  TypeScript
关注(0)|答案(2)|浏览(120)

TypeScript版本: 2.9.0-dev.20180414
代码

  1. declare function watch<T>(obj: T, propertyChangedCallbacks: { [K in keyof T]: (obj: T) => void }): void;
  2. watch({ x: 0 }, {
  3. x: obj => {},
  4. });

预期行为:

obj{ x: number }

实际行为:

src/a.ts(3,8): error TS7006: Parameter 'obj' implicitly has an 'any' type.

p1tboqfb

p1tboqfb1#

这也发生在2.8.1上:

  1. type Events = {
  2. ready: () => void
  3. error: (e: Error) => void
  4. reconnecting: (params: { attempt: number, delay: number}) => void
  5. }
  6. type RedisClient = {
  7. on<E extends keyof Events>(event: E, f: Events[E]): void
  8. }
  9. let c: RedisClient
  10. c.on('ready', () => 4) // BAD - arity is propagated, return type is any
  11. c.on('error', e => 4) // BAD - e is any
  12. c.on('reconnecting', p => p.attempt) // BAD - p is any

Playground

展开查看全部
xmjla07d

xmjla07d2#

我刚刚在TS playground中检查了这个问题,看起来这个问题已经修复了(自v3.5.1版本起)

相关问题