refactor: fix pipe type

An explicit cast was removed in fa9bce0e9a (diff-29e41c6172a2d68db9886be9e933ed2b477732095313e104c52a725f887276cf) which relies on [control flow narrowing](https://devblogs.microsoft.com/typescript/announcing-typescript-5-5/#control-flow-narrowing-for-constant-indexed-accesses) in TS 5.5. However, google3 is still on TS 5.4, so we can't fully depend on that feature. Using an intermediate variable makes the type narrowing work correctly even in TS 5.4.
This commit is contained in:
Doug Parker 2024-06-27 10:48:07 -07:00 committed by Douglas Parker
parent 7c4e0918d9
commit 79821006b8

View File

@ -133,12 +133,13 @@ export function applyPathTemplate<T extends PathTemplateData>(
if (!(pipe in data)) {
throw new UnknownPipeException(pipe);
}
if (typeof data[pipe] != 'function') {
const pipeFn = data[pipe];
if (typeof pipeFn != 'function') {
throw new InvalidPipeException(pipe);
}
// Coerce to string.
return '' + data[pipe](acc);
return '' + pipeFn(acc);
}, '' + replacement);
}