Giter VIP home page Giter VIP logo

ts-enums's Introduction

ngLanceFinney

After more than a decade focused on the Java desktop world, I've been in the Angular/JavaScript/TypeScript space since 2014.

I develop on Angular projects (and mentor junior Angular developers) with Cisco, and I used to be an instructor with Angular Boot Camp. You may have seen me speak occasionally at Angular conferences or Meetups.

http://pronoun.is/he

ts-enums's People

Contributors

johnjbarton avatar lmfinney avatar pbakondy avatar rauschma avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

ts-enums's Issues

Ordinal order isn't guaranteed

Unless I'm mistaken, it looks like you are relying on getOwnPropertyNames to determine the order of the enum values from which you set the ordinal. I'm pretty sure the order of that array is not guaranteed by the spec and it's possible to get different orderings on different platforms. This could be disastrous if you were using the ordinal on both a web-browser and a server and expecting them to match.

I didn't dig into the code, but the original Enumify library took the list of value names as an array in the initEnum function. Perhaps you should do something similar and require the programmer to pass in the values of the enum as an array in the order in which they should be defined. Then the ordinal only changes if the programmer changes it.

At a minimum, you might want to note in your documentation that the ordinal isn't guaranteed between platforms (or who knows, maybe even between separate invocations on the same platform).

How to make a generic?

@Enum("text")
export class PeriodUnit extends EnumType<PeriodUnit>() {

    static readonly DAY = new PeriodUnit('d');

    private constructor(
        readonly text: string,
    ) {
        super()
    }
}
export function EnumProp<T extends IStaticEnum<T> & { text: string }>(
    enumCls: T,
    options?: PropOptions
) {
    return (target, propertyKey) => {
        Prop({
            ...(options as object),
            type: String,
            set: (v: T) => v.text,
            get: (v: string) => enumCls.valueOf(v)
        })(target, propertyKey);
    }
}
@Schema()
export class Period {

    @Prop({ required: true })
    value: number

    @EnumProp(PeriodUnit,{
        required: true
    })
    unit: PeriodUnit
}

Argument of type 'typeof PeriodUnit' is not assignable to parameter of type 'IStaticEnum<typeof PeriodUnit> & { text: string; }'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.