Open rajesh99 opened 5 years ago
Constants are still not supported. I would expose those constants to clients using REST API for example using this JSON class:
public class ServerConstantsJson {
public final int transactRetries = ServerConstants.TRANSACT_RETRIES;
public final int maxErrorRowSize = ServerConstants.MAX_ERROR_ROW_SIZE;
}
Clients can then download these constants if they need them.
If you really want to generate them to TypeScript file you can use workaround with enum like this:
public enum ServerConstants {
TRANSACT_RETRIES(3),
MAX_ERROR_ROW_SIZE(50),
STRING_CONSTANT("hello");
@JsonValue
private final Object value;
private ServerConstants(Object value) {
this.value = value;
}
}
which should produce following TypeScript code:
export const enum ServerConstants {
TRANSACT_RETRIES = 3,
MAX_ERROR_ROW_SIZE = 50,
STRING_CONSTANT = "hello",
}
If you don't want to use @JsonValue
annotation you can use toString()
method (in this case you need to set enumsUsingToString
parameter to true
):
public enum ServerConstants {
TRANSACT_RETRIES(3),
MAX_ERROR_ROW_SIZE(50),
STRING_CONSTANT("hello");
private final Object value;
private ServerConstants(Object value) {
this.value = value;
}
@Override
public String toString() {
return String.valueOf(value);
}
}
But in this case all constants will be of string
type:
export const enum ServerConstants {
TRANSACT_RETRIES = "3", // string!
MAX_ERROR_ROW_SIZE = "50", // string!
STRING_CONSTANT = "hello",
}
Example Maven configuration:
<configuration>
<jsonLibrary>jackson2</jsonLibrary>
<outputFileType>implementationFile</outputFileType>
<outputKind>module</outputKind>
<mapEnum>asEnum</mapEnum>
<jackson2Configuration>
<enumsUsingToString>true</enumsUsingToString>
</jackson2Configuration>
...
</configuration>
You can also use nonConstEnums
parameter depending on your needs.
We are generating constants using a custom extension. Eg.
public interface Car {
public static final String DEFAULT_COLOR = "red";
String getColor();
}
This generates two TS files, one named Car
created by the default generator. And one named CarConstants
containing the Java constant as a TS const enum.
@vojtechhabarta What would make this a little bit more clean is, if EmitterExtensions
could be executed for each TsDeclarationModel
.
Any plans to include this feature or ask danielkaneider for a contribution?
@vojtechhabarta is there a technical issue at play here? In theory it looks like a one-to-one mapping of static final to static const, are there any edge cases?
Edit if anyone wants to do this on the CLI, I just made a prepublish script to get them grep -rh "public static final String" ./src/main/ | sed "s/ public static final String/export const/" >> ./build/ts/index.d.ts
@linusnorton this only makes sense when generating classes, I would like to have some solution also for interfaces (which need to be generated in case of d.ts
files). Maybe generating enum out of these constants could work in both cases.
Your script takes all String
constants in all files and put them to top level in TypeScript file? I thought you would want to have them in corresponding TypeScript classes.
What I've done is a bit of a hack but it works for my use case. I think the best solution would be an enum as you suggest
Even global const would be better than nothing :). But automatic ts enums would be more elegant, of course. On the Java side I cannot use enums for several cases, because the values need to be usable in annotations. So, generating something from actual Java constants is important for me.
This feature is really required to avoid code duplication for java and typescript. Going with enums is really a workaround and loses the readability of the java code. As mentioned, having global const is better than nothing. Appreciate some easy solutions and configurations with this project.
On Mon, Mar 15, 2021 at 1:54 PM Christian @.***> wrote:
Even global const would be better than nothing :). But automatic ts enums would be more elegant, of course. On the Java side I cannot use enums for several cases, because the values need to be usable in annotations. So, generating something from actual Java constants is important for me.
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://mailtrack.io/trace/link/f49325b421ee38a04aafc270ba9d3286a01ca249?url=https%3A%2F%2Fgithub.com%2Fvojtechhabarta%2Ftypescript-generator%2Fissues%2F426%23issuecomment-799218282&userId=6615365&signature=9e92832bae2b0511, or unsubscribe https://mailtrack.io/trace/link/a92ba19eef5bcedf4fdb2268c860c6a2d3e2bd07?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FACMX2DB42XA7W7P3GCGKEQLTDW75FANCNFSM4JCPMAQQ&userId=6615365&signature=42958c4942881a59 .
-- Rajesh www.servicefolder.com https://mailtrack.io/trace/link/de668aa3305ba6cf37d89f8f9060767ae4d2e87e?url=http%3A%2F%2Fwww.servicefolder.com&userId=6615365&signature=570d7eff911021d2 Field Service Software on Google Cloud Platform and Mobile
There is also question which constants to process apart from String
ones. If generating enums it's probably well defined (String
and Number
).
This seems not a good idea to use a TypeScript enum is such a situation.
Here is the TypeScript documentation about enums: https://www.typescriptlang.org/docs/handbook/enums.html
Generating String and Number constant into a TypeScript enum will lead to heterogeneous enums that seems discouraged by the above documentation.
Nothing forbids Java to have the same value for multiple constant in the same class. This will generates a TypeScript enum with same value which will cause trouble to feature like "Reverse mappings" for a numeric enum.
Example:
public class TimeConst {
public static final int NUMBER_OF_SECONDS_IN_MINUTE = 60;
public static final int NUMBER_OF_MINUTES_IN_HOUR = 60;
}
I think that this must not be generated as an enum.
If this is not an enum, the generator can probably support more constant types: String
, number
, char
, boolean
, enum
.
Maybe the following TypeScript class:
export class TimeConst {
static NUMBER_OF_SECONDS_IN_MINUTE = 60;
static NUMBER_OF_MINUTES_IN_HOUR = 60;
}
as suggested by @rajesh99 too is better for that use case.
@jjoslet you are absolutely right.
I wouldn't mind generating heterogenous enums if we have a reason for it but for number constants it is really not a good idea. So we would need to limit those enums for string constants in which case it doesn't make sense to introduce this feature (because we would still need another feature for numbers and possibly other types).
What I liked about the solution with enums was that they could be also used in d.ts files (as string literal unions or const enums). Do we have other possibility which could be used also in d.ts files?
I have the following java class in my code
I am looking for equivalent ts file like the following
I am following this project every 2-3 months, and I am improving in its understanding. Is this possible now.?
Lately, I am seeing many improvements with enums in this project. Can I use enums here? What extension is to be used?