Transition继承 CSSTransitionProps 编译错误

来源:7-9 拿来主义 - 自定义 Transition 组件编码第二部分

布鲁斯基

2020-10-19

问题

版本:

  • @types/react-transition-group@4.4.0
  • react-transition-group@4.4.1
  • react@16.14.0
  • typescript@3.7.2

在 7-8 Transition 组件课中, 继承 CSSTransitionProps时, 按照老师的写法用 interface 继承, typescript 编译报错, 并且提示 属性不存在.

interface TransitionProps extends CSSTransitionProps {
  animation?: AnimationName,
  wrapper? : boolean,
}

IDE 警告:

An interface can only extend an object type or intersection of object types with statically known members.

使用组件也会出现 ts 的错误

Property 'in' does not exist on type 'IntrinsicAttributes & TransitionProps & { children?: ReactNode; }'.
type TransitionProps = CSSTransitionProps & {
  animation?: AnimationName,
}

改 interface 为 type 别名就可以了, 想请问老师这是什么原理啊?

写回答

1回答

张轩

2020-10-20

同学你好 请看常见问题欧 https://coding.imooc.com/learn/questiondetail/176079.html

解释的非常清楚

3
1
布鲁斯基
噢噢! 一是@types定义变了, 二是 ts 层面 Interface 只能继承 with statically known members, 这个是说 声明的type 有动态的 member 么?
2020-10-20
共1条回复

React18+TS高仿AntD从零到一打造组件库

设计,开发,测试,发布再到 CI/CD,从0到1造轮子

2123 学习 · 959 问题

查看课程