Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can not generate default value for using enum in class #336

Open
Saul-Mirone opened this issue Dec 11, 2019 · 5 comments · May be fixed by #600
Open

Can not generate default value for using enum in class #336

Saul-Mirone opened this issue Dec 11, 2019 · 5 comments · May be fixed by #600

Comments

@Saul-Mirone
Copy link

enum Fruit {
  Apple = 'apple',
  Orange = 'orange'
}
class Test {
  fruit: Fruit = Fruit.Apple;
}

Then I get:

{
  fruit: {
    enum: ['apple', 'orange'],
    type: 'string',
  }
}

I expect the default can be set in here.

@adampenn
Copy link

adampenn commented Jan 9, 2020

I am having this same issue. Any updated @Saul-Mirone

@mohsinamjad
Copy link

having same issue default values are not being reflected in json-schema.

@adiliqbl
Copy link

Facing the same issue. Any updates or workaround for this?

@ahochsteger
Copy link
Contributor

In addition to enums the handling of default values is broken for constants too.
While this produces the default value in the schema:

class Test {
  num = 10;
}

This does not:

const DEFAULT_NUM = 10;
class Test {
  num = DEFAULT_NUM;
}

ahochsteger added a commit to ahochsteger/typescript-json-schema that referenced this issue May 2, 2024
…#336)

* add helper functions getDeclarationValue() and getInitializerValue()
* make user symbols available to sandbox evaluation
* simplify initializer logic (eliminate ifs)
@ahochsteger
Copy link
Contributor

The PR #600 implements support for referencing enums and constants as default values, would be great if I could get some feedback on it.
See test/programs/default-properties-ref for a test case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
6 participants