const PACT_TAGS: string[] = [];
const CI_BRANCH: string | undefined = process.env.CI_BRANCH;
const CI_USER: string | undefined = process.env.CI_USER;
const CI_COMMIT: string | undefined = process.env.CI_COMMIT;
const isCiPipeline = CI_BRANCH !== undefined && CI_USER !== undefined && CI_COMMIT !== undefined;
if (isCiPipeline) {
PACT_TAGS.push(CI_COMMIT); // <- error
PACT_TAGS.push(CI_BRANCH); // <- error
PACT_TAGS.push(CI_USER); // <- error
}
An issue has been encountered:
const CI_COMMIT: string | undefined
Argument of type 'string | undefined' is not assignable to parameter of type 'string'.
Type 'undefined' is not assignable to type 'string'.ts(2345)
The intention here is clear. The lines within the if clause are only meant to be executed if the specified variables are strings. While I could explicitly cast them as <string>
, doing so would defeat the purpose of using TypeScript.