The regression below is shown in a small TypeScript module that prepares a preview of an email campaign audience. The module is supposed to request template data, build a local draft for editing, and safely expose a snapshot to the outside. After a recent change, the preview starts behaving unpredictably when the same template is reused and when external code works with the returned result.
buildAudiencePreview function accepts templateId: string and fetchTemplate: (id: string) => Promise<unknown>.templateId must throw an exception with the text templateId is required.id field is a non-empty string;name field is a string;segments field is an array of objects;segments item has a non-empty string code and an array of strings tags.templateId: stringaudienceName: stringsegmentCodes: string[]segments: { code: string; tags: string[] }[]templateId.buildAudiencePreview(" spring-sale ", fetchTemplate) and save the result.result.segments[0].tags.push("vip") or result.segmentCodes.push("extra").buildAudiencePreview("spring-sale", fetchTemplate) again.result.segments[0].code and verify that this is also reflected in subsequent responses from the cache.Example source for reproduction:
const fetchTemplate = async () => ({
id: "spring-sale",
name: "Spring Sale",
segments: [
{ code: "new-users", tags: ["email", "promo"] },
{ code: "loyal", tags: ["discount"] }
]
});
templateId must be consistent between input validation, the request, and the cache.Invalid template payload.22.x5.8ES modulestsconfig: strict=truetsc --noEmitfetchTemplate is an async function with no hidden side effects except returning a payloadUse line comments to explain the problem, its impact, the fix and how to test it.