{"record":{"id":"f5a6eabeef09660d","repo":"anomalyco/sst","slug":"an-undefined-link-was-passed-into-a-link-array","errorCode":null,"errorMessage":"An undefined link was passed into a `link` array.","messagePattern":"An undefined link was passed into a `link` array\\.","errorType":"validation","errorClass":"VisibleError","httpStatus":null,"severity":"error","filePath":"platform/src/components/link.ts","lineNumber":110,"sourceCode":"        props: args.props,\n      };\n    });\n  }\n\n  export interface Linkable {\n    urn: Output<string>;\n    getSSTLink(): Definition;\n  }\n\n  export function isLinkable(obj: any): obj is Linkable {\n    return \"getSSTLink\" in obj;\n  }\n\n  export function build(links: any[]) {\n    return links\n      .map((link) => {\n        if (!link)\n          throw new VisibleError(\n            \"An undefined link was passed into a `link` array.\",\n          );\n        return link;\n      })\n      .filter((l) => isLinkable(l))\n      .map((l: Linkable) => {\n        const link = l.getSSTLink();\n        return all([l.urn, link]).apply(([urn, link]) => ({\n          name: urn.split(\"::\").at(-1)!,\n          properties: {\n            ...link.properties,\n            type: normalizeType(urn.split(\"::\").at(-2)!),\n          },\n        }));\n      });\n  }\n\n  export function getProperties(links?: Input<any[]>) {","sourceCodeStart":92,"sourceCodeEnd":128,"githubUrl":"https://github.com/anomalyco/sst/blob/a0bd20f762883e72a35caccb4896c42ce5b3f707/platform/src/components/link.ts#L92-L128","documentation":"`Link.build()` takes an array of resources passed to a component's `link` property and converts linkable ones into link definitions. Before filtering, it validates each array element; if any element is falsy (undefined/null), SST throws this VisibleError instead of silently dropping the broken entry, since it almost always indicates a mistake in the app config.","triggerScenarios":"Passing an array containing undefined/null into a `link` option, e.g. `new sst.aws.Function(\"Api\", { link: [bucket, missingSecret] })` where `missingSecret` is undefined — commonly from a conditional like `link: [stage === \"prod\" && secret].filter(Boolean)` without the filter, or referencing a resource created behind an if-branch.","commonSituations":"Conditionally created resources where the component variable stays undefined; typos in variable names resolved to undefined (no TS error if typed as any); destructuring an object with a missing key; a ternary that yields undefined instead of an empty array element.","solutions":["Find the undefined element: log or inspect the `link` array passed to the failing component.","Fix the conditional so the resource is always created, or use `.filter(Boolean)` to drop undefined entries before passing.","If the resource should be optional, build the array conditionally: `link: [...(secret ? [secret] : [])]`.","Enable TypeScript strictness so an undefined reference is caught at compile time."],"exampleFix":"// before\nconst secret = stage === \"prod\" ? new sst.Secret(\"MySecret\") : undefined;\nnew sst.aws.Function(\"Api\", { link: [secret] });\n// after\nconst secret = stage === \"prod\" ? new sst.Secret(\"MySecret\") : undefined;\nnew sst.aws.Function(\"Api\", { link: [secret].filter(Boolean) });","handlingStrategy":"type-guard","validationCode":"// Before passing a link array\nfunction validLinks(links: unknown[]) {\n  return links.filter((l): l is NonNullable<typeof l> => !!l);\n}\nnew sst.aws.Function(\"Api\", { link: validLinks([bucket, secret]) });","typeGuard":"function isLinkableResource(l: unknown): l is sst.Link.Linkable {\n  return !!l && typeof l === \"object\" && \"getSSTLink\" in l;\n}","tryCatchPattern":"try {\n  const fn = new sst.aws.Function(\"Api\", { link: links });\n} catch (e) {\n  if (String(e).includes(\"undefined link\")) {\n    console.error(\"A link entry is undefined — check conditional resource creation:\", links);\n  }\n  throw e;\n}","preventionTips":["Use `.filter(Boolean)` on arrays built from conditional resources","Type link arrays as `sst.Link.Linkable[]` so undefined elements fail typecheck","Avoid `cond && resource` patterns in link arrays without filtering","Enable strict TypeScript to catch undefined references at compile time"],"tags":["sst","linking","configuration","undefined"],"backgroundTag":"undefined-link-in-array","analyzedSha":"a0bd20f762883e72a35caccb4896c42ce5b3f707","analyzedAt":"2026-08-30T11:26:00.383Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}