I want to use any type in typescript, but generator gives me object type.
From OpenAPI 3 Data Types (section Any Type)
A schema without a type matches any data type – numbers, strings, objects, and so on.
4.2.3
{
"openapi": "3.0.2",
"info": {
"title": "Api",
"description": "Api",
"version": "1.0.0"
},
"paths": {},
"components": {
"schemas": {
"DataBlock": {
"title": "DataBlock",
"required": [
"data"
],
"type": "object",
"properties": {
"version": {
"title": "Version",
"type": "string",
"description": "Version of data block"
},
"data": {
"title": "Data",
"description": "Block data",
"nullable": true,
"example": "here may be any json value"
}
},
"description": "Data block with version"
}
}
}
}
openapi-generator generate -g typescript-axios -i ./src/api/openapi.json -o ./src/api/ -c ./src/api/.openapi-generator/config.yaml -DskipFormModel=true
Do not specify the type in the schema, and generate
/**
* Data block with version
* @export
* @interface DataBlock
*/
export interface DataBlock {
/**
* Version of data block
* @type {string}
* @memberof DataBlock
*/
version?: string;
/**
* Block data
* @type {object}
* @memberof DataBlock
*/
data: object | null;
}
/**
* Data block with version
* @export
* @interface DataBlock
*/
export interface DataBlock {
/**
* Version of data block
* @type {string}
* @memberof DataBlock
*/
version?: string;
/**
* Block data
* @type {any}
* @memberof DataBlock
*/
data: any; // <----- Any type
}
Not found
I have no idea
👍 Thanks for opening this issue!
🏷 I have applied any labels matching special text in your issue.
The team will review the labels and make any necessary changes.
I agree object type (default) is not helpful in Typescript. In fact, the same issue also exists for other typescript generators (e.g. typescript-angular ), not just typescript-axios.
This can be solved at your end by providing a custom typemapping via cli arguments:
openapi-generator generate ...other-args... --type-mappings object=any
This should probably be a default mapping for the typescript generators.
Maybe even unknown instead of any - for better type safety (to enforce users to always verify the shape of data at runtime)
Most helpful comment
I agree
objecttype (default) is not helpful in Typescript. In fact, the same issue also exists for other typescript generators (e.g.typescript-angular), not just typescript-axios.This can be solved at your end by providing a custom typemapping via cli arguments:
This should probably be a default mapping for the typescript generators.
Maybe even
unknowninstead ofany- for better type safety (to enforce users to always verify the shape of data at runtime)