V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Kei001
V2EX  ›  TypeScript

Ts+React 中装饰器使用问题

  •  
  •   Kei001 · 2022-04-15 10:25:50 +08:00 · 1366 次点击
    这是一个创建于 735 天前的主题,其中的信息可能已经有所发展或是发生改变。

    使用 withRouter 给组件添加路由属性和函数,感觉装饰器的语法更优雅,但是 ts 下 withRouter 装饰暴露组件会出现 ts 校验错误。代码如下:

    
    import React, { Component } from 'react';
    import { withRouter, RouteComponentProps } from 'react-router-dom';
    
    type Props = {} & RouteComponentProps;
    type State = {};
    
    @withRouter
    export default class Header extends Component<Props, State> {
      render() {
        console.log('Header 组件的 Props:', this.props);
        return <h1>This is Header</h1>;
      }
    }
    
    

    IDE 截图:

    截图.png

    看编辑器提示,似乎是一个 issue ,让使用 withRouter()函数调用模式,感觉有点不死心,目前没有很好的解决方案了吗?

    3 条回复    2022-04-15 23:34:57 +08:00
    mxT52CRuqR6o5
        1
    mxT52CRuqR6o5  
       2022-04-15 10:32:45 +08:00 via Android   ❤️ 1
    不用就是了,ts 目前的装饰器实现也和 es 标准不一样,为啥非要用呢(像 angular 里的装饰器也是当注解用的,会在编译时做一些特殊处理)
    Kei001
        2
    Kei001  
    OP
       2022-04-15 14:07:52 +08:00
    原来 ts 里装饰器的标准不一样,了解了!
    Charrlles
        3
    Charrlles  
       2022-04-15 23:34:57 +08:00 via iPhone
    Decorator 的提案进 stage 3 了,估计 ts 很快就要跟进改掉,还是继续等等吧
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2758 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 12:24 · PVG 20:24 · LAX 05:24 · JFK 08:24
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.