question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to use Enums with Props?

See original GitHub issue

Is 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:closed
  • Created 4 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

6reactions
drewcookcommented, Sep 30, 2021

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:

<script lang="ts">
  enum Layouts {
    Small = 'small',
    Medium = 'medium',
    Large = 'large'
  }
  
  enum Themes {
    Light = "light",
    Grey = "grey",
    Dark = "dark"
  }
  
  export default {
    name: 'Layout',
    props: {
      layout: {
        type: String as () => Layouts // cast it
        default: Layouts.Medium
      },
      theme: {
        type: String as () => Theme // cast it
        default: Theme.Light
      },
    },
  }
</script>

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.

6reactions
kaorun343commented, May 15, 2019

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.

enum Fruit {
    Apple = 'Apple',
    Banana = 'Banana',
}
function EnumProp<T extends string | number>(d: T, e: Record<T, any>) {
    return {
        default: d,
        validator(v: T) {
            return e[v] !== undefined
        }
    }
}

@Component
class YourComponent extends Vue {
  @Prop(EnumProp(Fruit.Apple, Fruit)) fruit!: Fruit
}
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found