How to use Enums with Props?
See original GitHub issueIs there any way to get type checking with Enums. I know that if I pass a invalid value, it will ignore it and apply the default which is nice. But I don’t get any type checking.
<template>
<section :class="theme">
<div class="container" :class="layout">
<slot/>
</div>
</section>
</template>
<script lang="ts">
import { Component, Prop, Vue } from "nuxt-property-decorator";
export enum Layouts {
Small = "small",
Medium = "medium",
Large = "large"
}
export enum Themes {
Light = "light",
Grey = "grey",
Dark = "dark"
}
@Component({})
export default class BaseSection extends Vue {
@Prop({ default: Layouts.Medium })
private layout!: Layouts;
@Prop({ default: Themes.Light }) private theme!: Themes;
}
</script>
<style scoped>
section {
font-family: "Montserrat", sans-serif;
}
.container {
padding: 0.5rem 2.5vw 1.5rem;
max-width: 90rem;
margin: 0 auto;
}
.small {
padding: 2rem 0;
}
.medium {
padding: 4rem 0;
}
.large {
padding: 6rem 0;
}
.light {
background-color: #fff;
}
.grey {
background-color: #e1e4ea;
}
.dark {
background-color: #010b19;
color: #fff;
}
</style>
Issue Analytics
- State:
- Created 4 years ago
- Comments:10 (5 by maintainers)
Top Results From Across the Web
How to use enum as props in react/typescript - Stack Overflow
You can just reference the enum value as you would in any other context: export enum Sizes{ Small, Large } export interface IProps{...
Read more >A Faster Approach to Enum Props in React - John Wright Stanly
Create quick interfaces for your React components through powerful TypeScript definitions ... Enums are one of the few features TypeScript adds to ...
Read more >Conditional rendering component using Enums in ReactJS
In the App.js file, we will create an enum object first. After that, we will add an 'enum' function to render components according...
Read more >Expose Enum Props in React - Jake Trent
The first method allows for quick, easy dev work. The second method allows for efficient tree shaking in the case of only exporting...
Read more >Opinionated React - Use Status Enums Instead of Booleans
A Better Approach - use Enums. An enum (short for enumeration), allows us to define a set of named constants. These constants can...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
If you are writing it in the SFC style with Vue 3, you can cast your prop type to a function that returns that of the enum type. This is how the syntax would look within the
<script>
tag:This way, you can work with your enums within TypeScript and allow the API of the component to support native strings as props when implementing it throughout the application.
This library only supports the features that is available at Vue itself.
Instead, you can define
EnumProp
function like this below to avoid writing a lot of boilerplate code.