Advertisement

Typescript下 Type ‘string | string[]‘ is not assignable to type ‘string | undefined‘

阅读量:

在采用TS( TypeScript)时,经常会遇到由于一些数据类型的特殊性而导致的编译错误问题。与常用的JS开发语言相比,在某些场景下可能会感到不适应。当调用antV的Reac组件进行绘图时,代码总是显示红色警告线。虽然在开发模式下不会影响程序的运行状态... 但在编译阶段仍会遇到问题。

在这里插入图片描述

在详细信息页面会显示**xxxx is not assignable to type xxxxx**,这表示Bar内的属性类型与当前配置不符,并且无法自动配置Bar内的属性类型。

即使用as方式给属性赋予类型。

复制代码
    const lineconfig = {
        data: linedata as any[],
        xField: 'year' as string,
        yField: 'gdp' as string,
        seriesField: 'name' as string,
        yAxis: {
            label: {
                formatter: (v: any) => `${v} 人`,
            },
        },
        legend: {
            position: 'top' as string,
        } as Object,
        smooth: true,
        // @TODO 后续会换一种动画方式
        animation: {
            appear: {
                animation: 'path-in' as string,
                duration: 5000 as number,
            },
        },
    };
    
    
    
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    
    代码解释

由于涉及第三方组件的定义,因此我们无法像使用**interface**那样为每个属性单独设定类型;而是必须采用上述方式来完成。

全部评论 (0)

还没有任何评论哟~