Closed littlespex closed 2 weeks ago
@cjpillsbury Anything to add on this topic? Do you have any experience documenting the as const
style?
Here are a few callouts:
Enums
usage are also discussed on this page)Derive and exporting the const "lookup object"/"map"'s type
export const CmcdStreamingFormat = {
DASH: 'd',
HLS: 'h',
SMOOTH: 's',
OTHER: 'o',
};
export type CmcdStreamingFormat = typeof CmcdStreamingFormat;
Get the value types to use as a type elsewhere
type ValueOf<T> = T[keyof T];
// Or whatever naming convention you prefer here
type CmcdStreamingFormats = ValueOf<CmcdStreamingFormat>;
const someFn = (streamingFormat: CmcdStreamingFormats) => {
// do stuff;
};
Typescript enums produce non-optimal code when compiled:
There is another way to express constrained sets using
as const
which produces more concise output, though it's more verbose to author:It would help reduce the code size of production apps using CML if we used the const style. Also of note, this would not constitute a breaking change, and would be transparent to users of CML. The only downside would be that APIs docs might not be as clean.