Skip to content

Instantly share code, notes, and snippets.

@NitsanBaleli
Created June 30, 2021 07:03
Show Gist options
  • Save NitsanBaleli/cbaad33ebd18d3ab29b38fd8f94787d0 to your computer and use it in GitHub Desktop.
Save NitsanBaleli/cbaad33ebd18d3ab29b38fd8f94787d0 to your computer and use it in GitHub Desktop.
typescript enum alternative
//https://stackoverflow.com/a/60041791/2460773
//https://www.typescriptlang.org/docs/handbook/enums.html#objects-vs-enums
export const mapConsumptionActiveMapType = {
elevation: "map",
orthomosaic: "elevation",
model_3d: "3d model",
} as const;
export type MapConsumptionActiveMapType = keyof typeof mapConsumptionActiveMapType;
let value: keyof typeof mapConsumptionActiveMapType;
for (value in mapConsumptionActiveMapType) {
const translationKey = mapConsumptionActiveMapType[value];
if (value === "model_3d" && !mapGroup.model_3d) return;
radios.push(
<PrRadio.Button value={value}>
{t(translationKey as DynamicTranslationKey)}
</PrRadio.Button>
);
}
@IlanFrumer
Copy link

IlanFrumer commented Jun 30, 2021

// global.d.ts

type Entries<T> = {
  [K in keyof T]: [K, T[K]]
}[keyof T][]

declare global {
  interface ObjectConstructor {
    entries<T>(obj: T): Entries<T>;
    keys<T, K = keyof T>(obj: T): K[];
  }
}

// Now can use:

const map = new Map(Object.entries(obj));
const values = new Set(Object.values(obj));
const keys = new Set(Object.keys(obj));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment