I have the following:
import * as core from '@actions/core'
import { GitHub } from '@actions/github'
import { context } from '@actions/github'
const valid_statuses = ["queued", "in_progress", "completed"] as const;
type Status = typeof valid_statuses[number]
function IsValidJson(str: string): boolean {
try {
JSON.parse(str)
} catch (e) {
return false
}
return true
}
function StringArrayFromJson(json: string): [string] {
if (json != null && typeof json === 'string' && !IsValidJson(json)) {
json = `[${ json }]`
}
if (json == null) {
json = '[]'
}
if (!IsValidJson(json)) {
throw new Error(`Invalid JSON: ${ json }`)
}
return JSON.parse(json) as [string]
}
async function main(): Promise<void> {
try {
let statuses = StringArrayFromJson(core.getInput('statuses', { required: true }))
//item in valid_statuses returns false!
if (statuses.length <= 0 || !statuses.every(item => { return item in valid_statuses })) {
core.setFailed(`ERROR: statuses must be an array of valid status strings. Got: ${ JSON.stringify(statuses) }`)
return
}
console.log('Success')
} catch (error) {
core.setFailed(error.message)
}
}
main()
I realize that code translates to the following Javascript:
const valid_statuses = ["queued", "in_progress", "completed"]
console.log("in_progress" in valid_statuses); //Returns false
How come I can't check if my string is a valid union?
EDIT (TSConfig):
{
"compilerOptions": {
"module": "commonjs",
"target": "esnext",
"lib": [
"es2015",
"es2017"
],
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitAny": true,
"removeComments": false,
"preserveConstEnums": true
},
"include": [
".github/actions/**/*.ts",
"**/*.ts"
],
"exclude": [
"node_modules"
]
}
EDIT (package.json):
{
"private": true,
"name": "create-check-action",
"scripts": {
"build": "tsc",
"test": "tsc --noEmit && jest"
},
"license": "ISC",
"dependencies": {
"@actions/core": "^1.2.4",
"@actions/github": "^2.2.0"
},
"devDependencies": {
"@types/jest": "^25.2.3",
"@types/node": "^14.0.5",
"jest": "^26.0.1",
"ts-jest": "^26.0.0",
"typescript": "^3.9.3"
}
}