Skip to content

Instantly share code, notes, and snippets.

@gangsthub
Created April 25, 2023 19:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gangsthub/fb5a62d72639729c23670b317e265b1b to your computer and use it in GitHub Desktop.
Save gangsthub/fb5a62d72639729c23670b317e265b1b to your computer and use it in GitHub Desktop.
Vue Props as enums
<script setup lang="ts">
enum VariantEnum {
PRIMARY = 'primary',
SECONDARY = 'secondary',
}
// Turn enums into string union types!
type TypeFromEnum<T> = T extends `${infer U}` ? U : never;
type VariantType = TypeFromEnum<VariantEnum>;
// ^? type VariantType = 'primary' | 'secondary'
withDefaults(
defineProps<{
variant: VariantType; // as a type 💫
}>(),
{
variant: VariantEnum.PRIMARY, // as a value ⭐️
}
);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment