fix: handle null value for experiments (#17584)

Fix https://github.com/coder/coder/issues/17583

**Relevant info**
- `option.value` can be `null`
- It is always better to use `unknown` instead of `any`, and use type
assertion functions as `Array.isArray()` before using/accessing object
properties and functions
This commit is contained in:
Bruno Quaresma
2025-04-28 11:12:49 -03:00
committed by GitHub
parent 0a26eeec0c
commit 5ca90aeb59
2 changed files with 18 additions and 6 deletions
@@ -120,6 +120,15 @@ describe("optionValue", () => {
additionalValues: ["single_tailnet"],
expected: { single_tailnet: true },
},
{
option: {
...defaultOption,
name: "Experiments",
value: null,
},
additionalValues: ["single_tailnet"],
expected: "",
},
{
option: {
...defaultOption,
@@ -40,8 +40,10 @@ export function optionValue(
case "Experiments": {
const experimentMap = additionalValues?.reduce<Record<string, boolean>>(
(acc, v) => {
// biome-ignore lint/suspicious/noExplicitAny: opt.value is any
acc[v] = (option.value as any).includes("*");
const isIncluded = Array.isArray(option.value)
? option.value.includes("*")
: false;
acc[v] = isIncluded;
return acc;
},
{},
@@ -57,10 +59,11 @@ export function optionValue(
// We show all experiments (including unsafe) that are currently enabled on a deployment
// but only show safe experiments that are not.
// biome-ignore lint/suspicious/noExplicitAny: opt.value is any
for (const v of option.value as any) {
if (v !== "*") {
experimentMap[v] = true;
if (Array.isArray(option.value)) {
for (const v of option.value) {
if (v !== "*") {
experimentMap[v] = true;
}
}
}
return experimentMap;