diff --git a/.changeset/fluffy-oranges-attend.md b/.changeset/fluffy-oranges-attend.md new file mode 100644 index 000000000..02ccc5c07 --- /dev/null +++ b/.changeset/fluffy-oranges-attend.md @@ -0,0 +1,5 @@ +--- +'renoun': patch +--- + +Fixes type aliases being inferred as components in `JavaScriptFileExport#getType`. diff --git a/packages/renoun/src/utils/resolve-type.test.ts b/packages/renoun/src/utils/resolve-type.test.ts index e543823f9..69dee184e 100644 --- a/packages/renoun/src/utils/resolve-type.test.ts +++ b/packages/renoun/src/utils/resolve-type.test.ts @@ -3034,7 +3034,7 @@ describe('resolveType', () => { expect(processedProperties).toMatchInlineSnapshot(` { "filePath": "test.ts", - "kind": "Component", + "kind": "Function", "name": "Text", "position": { "end": { @@ -3050,28 +3050,30 @@ describe('resolveType', () => { { "filePath": "test.ts", "generics": [], - "kind": "ComponentSignature", + "kind": "FunctionSignature", "modifier": undefined, - "parameter": { - "context": "parameter", - "defaultValue": undefined, - "description": undefined, - "filePath": "test.ts", - "isOptional": false, - "kind": "Reference", - "name": "props", - "position": { - "end": { - "column": 37, - "line": 8, - }, - "start": { - "column": 21, - "line": 8, + "parameters": [ + { + "context": "parameter", + "defaultValue": undefined, + "description": undefined, + "filePath": "test.ts", + "isOptional": false, + "kind": "Reference", + "name": "props", + "position": { + "end": { + "column": 37, + "line": 8, + }, + "start": { + "column": 21, + "line": 8, + }, }, + "text": "TextProps", }, - "text": "TextProps", - }, + ], "position": { "end": { "column": 46, @@ -9777,4 +9779,271 @@ describe('resolveType', () => { } `) }) + + test('overloads', () => { + const sourceFile = project.createSourceFile( + 'test.ts', + dedent` + type Loader = (path: string) => Promise; + + type Schema> = { + [Key in keyof Types]: (value: Types[Key]) => boolean | void; + }; + + /** A loader function. */ + function withSchema(loader: Loader): Loader; + + /** A schema and a loader function. */ + function withSchema>( + schema: Schema, + loader: Loader<{ [Key in keyof Types]: Types[Key] }> + ): Loader<{ [Key in keyof Types]: Types[Key] }>; + + /** Implementation of withSchema handling both overloads. */ + function withSchema>( + a: Schema | Loader, + b?: Loader + ): Loader { + return undefined as any + } + `, + { overwrite: true } + ) + const functionDeclaration = sourceFile.getFunctionOrThrow('withSchema') + const types = resolveType( + functionDeclaration.getType(), + functionDeclaration + ) + + expect(types).toMatchInlineSnapshot(` + { + "description": "Implementation of withSchema handling both overloads.", + "filePath": "test.ts", + "kind": "Function", + "name": "withSchema", + "position": { + "end": { + "column": 2, + "line": 22, + }, + "start": { + "column": 1, + "line": 17, + }, + }, + "signatures": [ + { + "description": "A loader function.", + "filePath": "test.ts", + "generics": [ + { + "constraint": undefined, + "defaultType": undefined, + "filePath": "test.ts", + "kind": "GenericParameter", + "name": "Types", + "position": { + "end": { + "column": 26, + "line": 8, + }, + "start": { + "column": 21, + "line": 8, + }, + }, + "text": "Types", + }, + ], + "kind": "FunctionSignature", + "modifier": undefined, + "parameters": [ + { + "context": "parameter", + "defaultValue": undefined, + "description": undefined, + "filePath": "test.ts", + "isOptional": false, + "kind": "Function", + "name": "loader", + "position": { + "end": { + "column": 49, + "line": 8, + }, + "start": { + "column": 28, + "line": 8, + }, + }, + "signatures": [ + { + "filePath": "test.ts", + "generics": [], + "kind": "FunctionSignature", + "modifier": undefined, + "parameters": [ + { + "context": "parameter", + "defaultValue": undefined, + "description": undefined, + "filePath": "test.ts", + "isOptional": false, + "kind": "String", + "name": "path", + "position": { + "end": { + "column": 35, + "line": 1, + }, + "start": { + "column": 23, + "line": 1, + }, + }, + "text": "string", + "value": undefined, + }, + ], + "position": { + "end": { + "column": 54, + "line": 1, + }, + "start": { + "column": 22, + "line": 1, + }, + }, + "returnType": "Promise", + "text": "(path: string) => Promise", + }, + ], + "text": "Loader", + }, + ], + "position": { + "end": { + "column": 66, + "line": 8, + }, + "start": { + "column": 1, + "line": 8, + }, + }, + "returnType": "Loader", + "tags": undefined, + "text": "function withSchema(loader: Loader): Loader", + }, + { + "description": "A schema and a loader function.", + "filePath": "test.ts", + "generics": [ + { + "constraint": undefined, + "defaultType": undefined, + "filePath": "test.ts", + "kind": "GenericParameter", + "name": "Types", + "position": { + "end": { + "column": 54, + "line": 11, + }, + "start": { + "column": 21, + "line": 11, + }, + }, + "text": "Types", + }, + ], + "kind": "FunctionSignature", + "modifier": undefined, + "parameters": [ + { + "context": "parameter", + "defaultValue": undefined, + "description": undefined, + "filePath": "test.ts", + "isOptional": false, + "kind": "Function", + "name": "loader", + "position": { + "end": { + "column": 55, + "line": 13, + }, + "start": { + "column": 3, + "line": 13, + }, + }, + "signatures": [ + { + "filePath": "test.ts", + "generics": [], + "kind": "FunctionSignature", + "modifier": undefined, + "parameters": [ + { + "context": "parameter", + "defaultValue": undefined, + "description": undefined, + "filePath": "test.ts", + "isOptional": false, + "kind": "String", + "name": "path", + "position": { + "end": { + "column": 35, + "line": 1, + }, + "start": { + "column": 23, + "line": 1, + }, + }, + "text": "string", + "value": undefined, + }, + ], + "position": { + "end": { + "column": 54, + "line": 1, + }, + "start": { + "column": 22, + "line": 1, + }, + }, + "returnType": "Promise<{ [Key in keyof Types]: Types[Key]; }>", + "text": "(path: string) => Promise<{ [Key in keyof Types]: Types[Key]; }>", + }, + ], + "text": "Loader<{ [Key in keyof Types]: Types[Key]; }>", + }, + ], + "position": { + "end": { + "column": 49, + "line": 14, + }, + "start": { + "column": 1, + "line": 11, + }, + }, + "returnType": "Loader<{ [Key in keyof Types]: Types[Key]; }>", + "tags": undefined, + "text": "function withSchema(loader: Loader<{ [Key in keyof Types]: Types[Key]; }>): Loader<{ [Key in keyof Types]: Types[Key]; }>", + }, + ], + "tags": undefined, + "text": "{ (loader: Loader): Loader; >(schema: Schema, loader: Loader<{ [Key in keyof Types]: Types[Key]; }>): Loader<{ [Key in keyof Types]: Types[Key]; }>; }", + } + `) + }) }) diff --git a/packages/renoun/src/utils/resolve-type.ts b/packages/renoun/src/utils/resolve-type.ts index 6c975cce6..3cace5b62 100644 --- a/packages/renoun/src/utils/resolve-type.ts +++ b/packages/renoun/src/utils/resolve-type.ts @@ -321,9 +321,10 @@ export function resolveType( keepReferences: boolean = false, dependencies?: Set ): ResolvedType | undefined { + const aliasSymbol = type.getAliasSymbol() const symbol = - /* First, attempt to get the aliased symbol for imported and aliased types */ - type.getAliasSymbol() || + /* First, attempt to get the aliased symbol for aliased types */ + aliasSymbol || /* Next, try to get the symbol of the type itself */ type.getSymbol() || /* Finally, try to get the symbol of the apparent type */ @@ -864,7 +865,10 @@ export function resolveType( dependencies ) - if (isComponent(symbolMetadata.name, resolvedCallSignatures)) { + if ( + aliasSymbol === undefined && + isComponent(symbolMetadata.name, resolvedCallSignatures) + ) { resolvedType = { kind: 'Component', name: symbolMetadata.name,