diff --git a/src/main/convert.js b/src/main/convert.js index faf8e34..b3ae25c 100644 --- a/src/main/convert.js +++ b/src/main/convert.js @@ -191,7 +191,7 @@ export class ThriftFileConverter { generateFunction = (fn: FunctionDefinition) => `${fn.id.name}: (${ - fn.fields.length ? this.generateStructContents([...fn.fields]) : '' + fn.fields.length ? this.generateStructContents([...fn.fields], true) : '' }) => ${this.convertType(fn.returns)}`; generateTypedef = (def: Typedef) => { @@ -362,18 +362,18 @@ export class ThriftFileConverter { generateStruct = ({id: {name}, fields}: Struct | Exception) => `export type ${name} = ${this.generateStructContents(fields)};`; - generateStructContents = (fields: Array) => - `{|${fields + generateStructContents = (fields: Array, readOnly?: boolean) => + `${readOnly === true ? '$ReadOnly<' : ''}{|${fields .map((field: Field) => { const valueType = field.valueType; let optionalPrefix = this.isOptional(field) ? '?' : ''; let value = valueType.type === 'Identifier' ? this.getIdentifier(valueType.name, 'type') - : this.convertType(valueType); - return `${field.name}${optionalPrefix}: ${optionalPrefix}${value};`; + : this.convertType(valueType, null, readOnly); + return `${field.name}${optionalPrefix}:${optionalPrefix}${value};`; }) - .join('\n')}|}`; + .join('\n')}|}${readOnly === true ? '>' : ''}`; generateUnion = ({id: {name}, fields}: Union) => `export type ${name} = ${this.generateUnionContents(fields)};`; @@ -557,7 +557,7 @@ export class ThriftFileConverter { return false; }; - convertBaseType(t: AstNode, def?: Definition): string | void { + convertBaseType(t: AstNode, def?: ?Definition): string | void { if (t.type !== 'BaseType') { return undefined; } @@ -605,7 +605,7 @@ export class ThriftFileConverter { return undefined; } - convertMapType(t: AstNode, def?: Definition): string | void { + convertMapType(t: AstNode, def?: ?Definition, readOnly?: ?boolean): string | void { if (t.type === 'Map' && def) { const valueType = this.convertType(t.valueType); if (def.type === 'Const' && def.value.type === 'ConstMap') { @@ -627,7 +627,7 @@ export class ThriftFileConverter { ); } } else if (entry.key.type === 'Literal') { - return `'${entry.key.value}': ${valueType}`; + return readOnly === true ? `'${entry.key.value}': $ReadOnly<${valueType}>` : `'${entry.key.value}': ${valueType}`; } else { throw new Error('unsupported'); } @@ -651,13 +651,13 @@ export class ThriftFileConverter { return this.identifiersTable[def.name].type === 'Enum'; } - convertType(t: AstNode, def?: Definition): string { + convertType(t: AstNode, def?: ?Definition, readOnly?: boolean): string { if (!t) { throw new Error(`Assertion failed. t is not defined.`); } let type: string | void = this.convertArrayType(t) || - this.convertMapType(t, def) || + this.convertMapType(t, def, readOnly) || this.convertEnumType(t) || this.convertBaseType(t, def); if (type !== undefined) {