Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v7] transform + nullable issue with Identifier with Node.js API #1688

Open
2 tasks done
depsimon opened this issue Jun 3, 2024 · 3 comments
Open
2 tasks done

[v7] transform + nullable issue with Identifier with Node.js API #1688

depsimon opened this issue Jun 3, 2024 · 3 comments
Labels
bug Something isn't working openapi-ts Relevant to the openapi-typescript library

Comments

@depsimon
Copy link

depsimon commented Jun 3, 2024

Description

I'm trying to use the docs example to transform the schema for Date & Blobs.

[{
	"resource": "/Users/simon/Code/WebApp/packages/sdk/openapi.ts",
	"owner": "typescript",
	"code": "2322",
	"severity": 8,
	"message": "Type '(schemaObject: SchemaObject) => Identifier | UnionTypeNode | undefined' is not assignable to type '(schemaObject: SchemaObject, options: TransformNodeOptions) => TypeNode | TransformObject | undefined'.\n  Type 'Identifier | UnionTypeNode | undefined' is not assignable to type 'TypeNode | TransformObject | undefined'.\n    Type 'Identifier' is not assignable to type 'TypeNode | TransformObject | undefined'.\n      Type 'Identifier' is not assignable to type 'TypeNode'.",
	"source": "ts",
	"startLineNumber": 12,
	"startColumn": 5,
	"endLineNumber": 12,
	"endColumn": 14
},{
	"resource": "/Users/simon/Code/WebApp/packages/sdk/openapi.ts",
	"owner": "typescript",
	"code": "2741",
	"severity": 8,
	"message": "Property '_typeNodeBrand' is missing in type 'Identifier' but required in type 'TypeNode'.",
	"source": "ts",
	"startLineNumber": 15,
	"startColumn": 45,
	"endLineNumber": 15,
	"endColumn": 49,
	"relatedInformation": [
		{
			"startLineNumber": 5204,
			"startColumn": 9,
			"endLineNumber": 5204,
			"endColumn": 23,
			"message": "'_typeNodeBrand' is declared here.",
			"resource": "/Users/simon/Code/WebApp/node_modules/.pnpm/[email protected]/node_modules/typescript/lib/typescript.d.ts"
		}
	]
},{
	"resource": "/Users/simon/Code/WebApp/packages/sdk/openapi.ts",
	"owner": "typescript",
	"code": "2322",
	"severity": 8,
	"message": "Type 'Identifier' is not assignable to type 'TypeNode'.",
	"source": "ts",
	"startLineNumber": 20,
	"startColumn": 45,
	"endLineNumber": 20,
	"endColumn": 49
}]
Name Version
openapi-typescript 7.0.0-rc.0
Node.js v20.11.1
Typescript v5.5.5
OS + version macOS 14.5

Reproduction

Here's my TS file I'm using to generate my SDK:

import fs from "node:fs";
import openapiTS, { astToString } from "openapi-typescript";
import ts from "typescript";

const BLOB = ts.factory.createIdentifier("Blob"); // `Blob`
const DATE = ts.factory.createIdentifier("Date"); // `Date`
const NULL = ts.factory.createLiteralTypeNode(ts.factory.createNull()); // `null`

const ast = await openapiTS(
  new URL(mySchema),
  {
    transform(schemaObject) {
      if (schemaObject.format === "date-time") {
        return schemaObject.nullable
          ? ts.factory.createUnionTypeNode([DATE, NULL])
          : DATE;
      }
      if (schemaObject.format === "binary") {
        return schemaObject.nullable
          ? ts.factory.createUnionTypeNode([BLOB, NULL])
          : BLOB;
      }
    },
  },
);

const contents = astToString(ast);
fs.writeFileSync("./src/api.ts", contents);

Expected result

No Typescript errors.

Checklist

@depsimon depsimon added bug Something isn't working openapi-ts Relevant to the openapi-typescript library labels Jun 3, 2024
@ziyuang
Copy link

ziyuang commented Jun 12, 2024

Do you think this is related: microsoft/TypeScript#40263

@Flax95
Copy link

Flax95 commented Jul 1, 2024

Did you manage to solve this? 🙂
I ran into the same problem when following the docs example.

@depsimon
Copy link
Author

depsimon commented Jul 1, 2024

Nope, I'm not using this at the moment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working openapi-ts Relevant to the openapi-typescript library
Projects
None yet
Development

No branches or pull requests

3 participants