typescript-json-schema icon indicating copy to clipboard operation
typescript-json-schema copied to clipboard

Can not generate default value for using enum in class

Open Saul-Mirone opened this issue 5 years ago • 5 comments

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.

Saul-Mirone avatar Dec 11 '19 04:12 Saul-Mirone

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

adampenn avatar Jan 09 '20 22:01 adampenn

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

mohsinamjad avatar Jan 11 '21 07:01 mohsinamjad

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

adiliqbl avatar Oct 26 '22 01:10 adiliqbl

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 avatar Apr 30 '23 08:04 ahochsteger

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.

ahochsteger avatar May 02 '24 04:05 ahochsteger