NoxelNyx
NoxelNyx

Reputation: 997

Types of property X are incompatible

I'm just starting to play around with Typescript and am having what seems to be an odd issue.

I have declared an interface like so:

Location.d.ts

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

This interface is then used like this:

reducer.tsx

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

Now, I am attempting to declare a variable with the type MapState, but I'm getting the error:

Type ... is not assignable to type 'MapState'. Types of property 'locations' are incompatible. Type ... is not assignable to type 'Location[]'. Type ... is not assignable to type 'Location'. Property 'hash' is missing in type ...

Here is my declaration:

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

If I explicitly cast the Location, then it works:

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

But this is terribly verbose. Am I missing something here?

Upvotes: 2

Views: 8802

Answers (1)

artem
artem

Reputation: 51809

Property 'hash' is missing in type

This means that Location in the place where the error happens is not your Location type - it's the type for browser location object defined in dom.d.ts

You probably did not import your Location type in that file (reducer.tsx) or there is some other reason why the compiler does not see it.

Upvotes: 3

Related Questions