Skip to content

tsgo eagerly resolve the type of inferred function Expression #2234

@brokenmass

Description

@brokenmass

TSGO eagerly resolve the type of inferred function Expression. for simplicity the example shows how keyof TestInterface is resolved to never (but is wrong as TestInterface could be augmented), but the issue is not limited to just keyof empty interfaces. The eager resolution happens to both params and return types of the function.

This leads to a whole set of issues (for example const getClient = (config: Simplify<AxiosConfig>) => ,,, would resolve config to a object literal that also use some internal types in axios making getClient not portable.

if the function expression type is explicitly defined the problem does not appear.

Steps to reproduce

export interface TestInterface {}
export const inferredFunction = function (): Record<keyof TestInterface, string> {return {} as any}
export const explicitFunction: () => Record<keyof TestInterface, string> = function (): Record<keyof TestInterface, string {return {} as any}
export const inferredArrowFunction = (): Record<keyof TestInterface, string> => ({}) as any;
export const explicitArrowFunction: () => Record<keyof TestInterface, string> = () => ({}) as any;
export function functionDeclaration() :Record<keyof TestInterface, string> { return {} as any}

Behavior with typescript@5.9

export interface TestInterface {}
export declare const inferredFunction: () => Record<keyof TestInterface, string>;
export declare const explicitFunction: () => Record<keyof TestInterface, string>;
export declare const inferredArrowFunction: () => Record<keyof TestInterface, string>;
export declare const explicitArrowFunction: () => Record<keyof TestInterface, string>;
export declare function functionDeclaration(): Record<keyof TestInterface, string>;

Behavior with tsgo

export interface TestInterface {}
export declare const inferredFunction: () => Record<never, string>;
export declare const explicitFunction: () => Record<keyof TestInterface, string>;
export declare const inferredArrowFunction: () => Record<never, string>;
export declare const explicitArrowFunction: () => Record<keyof TestInterface, string>;
export declare function functionDeclaration(): Record<keyof TestInterface, string>;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions