Suddenly getting Typescript error TS2322, type not assignable errors during build process

Viewed 404

I have APIs written in lambda, exposed via API Gateway using Koa. I use AWS Codebuild for my deployment.

Recently Codebuild has started throwing the following error when the build process starts:

src/components/chart-color-settings/chart-color-settings.ts(11,13): error TS2322: Type 'string | string[] | undefined' is not assignable to type 'string | undefined'.
  Type 'string[]' is not assignable to type 'string'.

It throws this error in some of the files where I am reading query param and passing it to a function.

For example

import { Context } from "koa";

static async getChartColorSetting(ctx: Context) {
    const handlerFactory = ChartColorSettingsHandlerFactory.getInstance(dbService);
    let chartType: string | undefined = ctx.request.query.chartType; //this is where it says the problem is 
    let result = await handlerFactory.getChartColorSetting(chartType)    
    //rest of the code
}

Earlier it never complained about this problem. Suddenly it has started failing the builds because it "thinks" it will receive either string[] in the ctx.request.query or the function accepts some other type of param

Nothing has changed in that code, also it is showing similar build failure errors in other places where I am reading from ctx.request.query and giving me "not assignable" errors

Codebuild error below: enter image description here

Whereas the actual code for the error it is talking about looks like this:

enter image description here

As you can see from the code screenshot from above that the method tranlateHolidayInShifts is expecting the right type of params as read via the ctx.request.query.

I have no idea to what has caused this all of a sudden.

Any ideas or suggestions to fix this once and for all so the builds don't keep failing.

Thank you.

2 Answers

Error said type string[] is not assignable to type 'string', So i think u can try declare ur variable from string to array of string:

let chartType: string[] | undefined = ctx.request.query.chartType;

The reason this error may have suddenly appeared is because you're not committing either the "package-lock.json" or "yarn.lock" files.

Related