Created
April 25, 2023 19:51
-
-
Save gangsthub/fb5a62d72639729c23670b317e265b1b to your computer and use it in GitHub Desktop.
Vue Props as enums
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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