Giter Site home page Giter Site logo

Comments (6)

ah-yu avatar ah-yu commented on September 23, 2024

Enum members can be either constant or computed values.

Typescript docs: https://www.typescriptlang.org/docs/handbook/enums.html#computed-and-constant-members
Example: https://www.typescriptlang.org/play/?#code/KYOwrgtgBAYg9nKBvAUFKBBANGqAhKAXigFkBDAFwAsA6AJzJABM4IAKAShQF8g

from biome.

jpike88 avatar jpike88 commented on September 23, 2024

My god... I didn't know that was possible.

It should be noted though that computed values are only allowed in numeric enums. And once computed they are fixed for the duration of runtime. So in that sense they behave as a constant and having a truthy if statement like that is almost always going to be an accident

from biome.

ah-yu avatar ah-yu commented on September 23, 2024

And once computed they are fixed for the duration of runtime

No, they are not fixed.

zheyzhang@HANM18365 react % cat foo.ts 
enum Foo {
    A,
    B = Math.random(),
}
console.log(Foo.B);
zheyzhang@HANM18365 react % deno run foo.ts 
0.2797788528053844
zheyzhang@HANM18365 react % deno run foo.ts
0.15004983018100404
zheyzhang@HANM18365 react % deno run foo.ts
0.8717746009202871
zheyzhang@HANM18365 react % 

from biome.

jpike88 avatar jpike88 commented on September 23, 2024

'for the duration of runtime' so in that sense they are fixed and behave like a constant as they are a readonly property.

Screenshot 2024-05-24 at 3 40 38 PM

from biome.

ah-yu avatar ah-yu commented on September 23, 2024

Sorry, I misunderstood.

While the value is determined at runtime, we cannot know it in advance. If the computed value is 0, it will be treated as false in an if statement.

from biome.

Conaclos avatar Conaclos commented on September 23, 2024

This is out of scope of the current rule. This requires type info.

from biome.

Related Issues (20)

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.