代码之家  ›  专栏  ›  技术社区  ›  Anonymous Creator

此条件将始终返回“false”,因为类型“0”和“2”没有重叠

  •  1
  • Anonymous Creator  · 技术社区  · 5 年前

    @Component()
    export default class Transfer extends Vue {
    
      @Prop({ default: [] })
      buttonTexts!: [];
      
      get hasButtonTexts() {
        return this.buttonTexts.length === 2; // Here it gives error.
      }
      
    }
    

    其误差如下:

    “此条件将始终返回'false',因为类型'0'和'2'没有重叠”

    1 回复  |  直到 5 年前
        1
  •  1
  •   T.J. Crowder    5 年前

    问题是你给的类型 buttonTexts : [] length 0 长度 长度 0 number (详见下文)。

    你几乎肯定想要 按钮文本

    buttonTexts!: string[]
    

    Playground link to pared-down code showing the issue .

    Playground link to example fix .


    []

    基本上,TypeScript允许您定义具有特定内容的数组类型。例如: [string] 是只包含一个字符串的数组。 [string, number] . (对于返回多个东西的函数来说,这很方便,比如React的 useState )同样, 长度 0 ,不是 ;您的类型 2 literal是文本类型 ;出现错误是因为类型 0 与类型没有重叠 2 .

    推荐文章