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

Closed
2 tasks done
depsimon opened this issue Jun 3, 2024 · 5 comments · Fixed by #1753
Closed
2 tasks done

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

depsimon opened this issue Jun 3, 2024 · 5 comments · Fixed by #1753
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.

@danmichaelo
Copy link
Contributor

Seems to work to wrap the Identifiers in ts.factory.createTypeReferenceNode:

const BLOB = ts.factory.createTypeReferenceNode(ts.factory.createIdentifier("Blob"));
const FILE = ts.factory.createTypeReferenceNode(ts.factory.createIdentifier("File"));
const NULL = ts.factory.createLiteralTypeNode(ts.factory.createNull());

Btw. I found out by using the TypeScript AST Viewer, where you can type in some TypeScript and see the factory method calls needed to generate that TypeScript: https://ts-ast-viewer.com/#code/C4TwDgpgBMEM7CgXigMQJYBsIB8B2ArppkA

image

@drwpow
Copy link
Contributor

drwpow commented Jul 8, 2024

Btw. I found out by using the TypeScript AST Viewer, where you can type in some TypeScript and see the factory method calls needed to generate that TypeScript: https://ts-ast-viewer.com/#code/C4TwDgpgBMEM7CgXigMQJYBsIB8B2ArppkA

Oh I hadn’t seen that website before! That’s very handy! Bookmarking it.

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

Successfully merging a pull request may close this issue.

5 participants