代码之家  ›  专栏  ›  技术社区  ›  NoxelNyx

属性x的类型不兼容

  •  1
  • NoxelNyx  · 技术社区  · 8 年前

    我刚开始玩弄打字稿,现在有一个似乎很奇怪的问题。

    我已经声明了这样的接口:

    Location.d.ts

    interface Location {
        lat: number;
        lng: number;
    }
    

    然后使用如下界面:

    reducer.tsx

    interface MapState extends State {
        locations: Location[];
    }
    

    现在,我正试图声明一个类型为 MapState ,但我得到了错误:

    类型…无法分配给类型“mapstate”。属性“locations”的类型不兼容。类型…无法分配给类型“location[]”。类型…不能分配给类型“location”。类型中缺少属性“hash”…

    这是我的声明:

    const initialState: MapState = {
        locations: [
            { lat: 33.488069, lng: -112.072972 }
        ]
    };
    

    如果我明确地 Location ,然后工作:

    const initialState: MapState = {
        locations: [
            ({ lat: 33.488069, lng: -112.072972 } as Location)
        ]
    };
    

    但这太冗长了。我是不是错过了什么?

    1 回复  |  直到 8 年前
        1
  •  3
  •   artem    8 年前

    Location dom.d.ts

    reducer.tsx

    推荐文章