js-to-ts-converter
js-to-ts-converter copied to clipboard
Added getTypesFromComment() function.
Allow property type metadata to be extracted from property comment.
Javascript input
export class Test {
constructor() {
// Types: [`TS Type` #TS Default# @TS IsNullable@ ^OA Type^ ~OA Format~] - https://regex101.com
// TypeScript Data Types - https://www.typescriptlang.org/docs/handbook/basic-types.html
// OpenAPI Data Types - https://swagger.io/docs/specification/data-models/data-types
// TS Type pattern: `` - (?<=`).*(?=`)
// TS Default pattern: ## - (?<=#).*(?=#)
// TS IsNullable pattern: @@ - (?<=@).*(?=@)
// OA Type pattern: ^^ - (?<=\^).*(?=\^)
// OA Format pattern: ~~ - (?<=~).*(?=~)
this.strProp = null; // Types: [`string` #'New Value'# @true@ ^string^ ~~] -
this.boolProp = null; // Types: [`boolean` #true# @true@ ^boolean^ ~~] -
// OpenAPI String - https://swagger.io/docs/specification/data-models/data-types/#string
this.dateProp = null; // Types: [`Date` ## @@ ^string^ ~date~] -
this.dateTimeProp = null; // Types: [`Date` ## @@ ^string^ ~date-time~] -
this.byteProp = null; // Types: [`string` ## @@ ^string^ ~byte~] -
this.binaryProp = null; // Types: [`string` ## @@ ^string^ ~binary~] -
this.emailProp = null; // Types: [`string` ## @@ ^string^ ~email~] -
// OpenAPI Numbers - https://swagger.io/docs/specification/data-models/data-types/#numbers
this.intProp = null; // Types: [`number` ## @@ ^integer^] -
this.int32Prop = null; // Types: [`number` ## @@ ^integer^ ~int32~] -
this.int64Prop = null; // Types: [`number` ## @@ ^integer^ ~int64~] -
this.floatProp = null; // Types: [`number` ## @@ ^number^ ~float~] -
this.doubleProp = null; // Types: [`number` ## @@ ^number^ ~double~] -
this.classProp = new TestRef(); // Types: [`TestRef` ## @@ ^object^ ~REF:TestRef~] - TestRef class
this.arrayofClassProp = [new TestRef()]; // Types: [`TestRef[]` ## @@ ^array^ ~REF:TestRef[]~] - Arrray of TestRef class
}
}
Typescript output:
export class Test {
public strProp: string | null = 'New Value';
public boolProp: boolean | null = true;
public dateProp: Date;
public dateTimeProp: Date;
public byteProp: string;
public binaryProp: string;
public emailProp: string;
public intProp: number;
public int32Prop: number;
public int64Prop: number;
public floatProp: number;
public doubleProp: number;
public classProp: TestRef;
public arrayofClassProp: TestRef[];
constructor() {
// Types: [`TS Type` #TS Default# @TS IsNullable@ ^OA Type^ ~OA Format~] - https://regex101.com
// TypeScript Data Types - https://www.typescriptlang.org/docs/handbook/basic-types.html
// OpenAPI Data Types - https://swagger.io/docs/specification/data-models/data-types
// TS Type pattern: `` - (?<=`).*(?=`)
// TS Default pattern: ## - (?<=#).*(?=#)
// TS IsNullable pattern: @@ - (?<=@).*(?=@)
// OA Type pattern: ^^ - (?<=\^).*(?=\^)
// OA Format pattern: ~~ - (?<=~).*(?=~)
this.strProp = null; // Types: [`string` #'New Value'# @true@ ^string^ ~~] -
this.boolProp = null; // Types: [`boolean` #true# @true@ ^boolean^ ~~] -
// OpenAPI String - https://swagger.io/docs/specification/data-models/data-types/#string
this.dateProp = null; // Types: [`Date` ## @@ ^string^ ~date~] -
this.dateTimeProp = null; // Types: [`Date` ## @@ ^string^ ~date-time~] -
this.byteProp = null; // Types: [`string` ## @@ ^string^ ~byte~] -
this.binaryProp = null; // Types: [`string` ## @@ ^string^ ~binary~] -
this.emailProp = null; // Types: [`string` ## @@ ^string^ ~email~] -
// OpenAPI Numbers - https://swagger.io/docs/specification/data-models/data-types/#numbers
this.intProp = null; // Types: [`number` ## @@ ^integer^] -
this.int32Prop = null; // Types: [`number` ## @@ ^integer^ ~int32~] -
this.int64Prop = null; // Types: [`number` ## @@ ^integer^ ~int64~] -
this.floatProp = null; // Types: [`number` ## @@ ^number^ ~float~] -
this.doubleProp = null; // Types: [`number` ## @@ ^number^ ~double~] -
this.classProp = new TestRef(); // Types: [`TestRef` ## @@ ^object^ ~REF:TestRef~] - TestRef class
this.arrayofClassProp = [new TestRef()]; // Types: [`TestRef[]` ## @@ ^array^ ~REF:TestRef[]~] - Arrray of TestRef class
}
Wow well this is interesting! Are those comments left by swagger generating files?
Btw, sorry for the delay in seeing this - just saw the email notification now!
Wow well this is interesting! Are those comments left by swagger generating files?
No. Hand generated ;-)
Updated PR with requested items.
Get constructor, getter, setter, methods, functions parameter and return types from JSDoc.