0

I want to define an object according to type. Within that object is an array consisting of elements that also correspond to a given type. However, I want to only push elements to this array later and initialize it empty for now. But it gives me an error and I can't figure this out. I have the following lines in my app.ts:

const publishObject: MqttPublishObject = {
                        IsTurnedOff: detail.isTurnedOff,
                        processType: detail.processType.name === "linear" ? 0 : 1,
                        anchorPoints: <MqttPublishAnchorPoint>[]
                      };

For which I get this errors:

src/app/app.ts(51,25): error TS2322: Type 'MqttPublishAnchorPoint' is not assignable to type 'any[]'.
src/app/app.ts(51,39): error TS2352: Conversion of type 'undefined[]' to type 'MqttPublishAnchorPoint' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.

The typedefs are in the following files:

mqttPublishObject.ts:

export interface MqttPublishObject {
   IsTurnedOff : boolean;
   processType : number;
   anchorPoints : MqttPublishAnchorPoint[];
}

mqttPublishAnchorPoint.ts

export interface MqttPublishAnchorPoint {
    hour: number;
    minute: number;
    second: number;
    intensity: number;
}

I feel I'm going about it the wrong way, but I don't see how. What exactly is the problem?

2
  • 2
    <MqttPublishAnchorPoint>[] means [] as MqttPublishAnchorPoint, not [] as MqttPublishAnchorPoint[]. This is probably the reason for error. Commented Feb 17, 2021 at 18:07
  • Ahh thank you! I solved it like this: <MqttPublishAnchorPoint[]>[]. If you care to answer the question I can make you the accepted answer. Commented Feb 18, 2021 at 9:36

1 Answer 1

1

The errors are essentially telling you two things:

  • First error means that you're trying to assign value of type MqttPublishAnchorPoint to the field of type MqttPublishAnchorPoint[]. It means that TypeScript somehow is treating your array not as an array, but as a MqttPublishAnchorPoint.
  • The reason for this is given by the second error: you're trying to cast some value of array type (undefined[]) to MqttPublishAnchorPoint, and TypeScript can't make any sense of this assertion, since types are non-overlapping.

The core reason for both these errors is the cast: <T>[] means [] as T, not [] as T[], i.e. the cast is applied to array, not to its inner type. The fix is simple:

const publishObject: MqttPublishObject = {
  IsTurnedOff: detail.isTurnedOff,
  processType: detail.processType.name === "linear" ? 0 : 1,
  anchorPoints: <MqttPublishAnchorPoint[]>[],
};

Playground (simplified by removing everything that doesn't matter for reproducing error).


However, in this case you don't ever need the cast. Since the variable is strongly typed, TypeScript is able to infer the array type without specifying it explicitly near the value:

const publishObject: MqttPublishObject = {
  IsTurnedOff: detail.isTurnedOff,
  processType: detail.processType.name === "linear" ? 0 : 1,
  anchorPoints: [], // inferred as MqttPublishAnchorPoint[]
};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.