Skip to content

Inconsistent type inferring behavior #53776

@chernodub

Description

@chernodub

Bug Report

Hi, we're having issues using inline call of the generic function decorator in a specific context. Unfortunately, I couldn't find anything related to this on stackoverflow, or here. Please check out the code below and feel free to let me know if more information/context is needed. Thanks!

🔎 Search Terms

  • infer generic
  • infer generic function

🕗 Version & Regression Information

This is the behavior in every version I tried, and I reviewed the FAQ for entries about inference and generics

⏯ Playground Link

Playground link with relevant code

💻 Code

function deferQuery<TData>({}: {
  queryFn: () => Promise<TData>;
  onSuccess: (data: TData) => void;
}) {
    // ...
}

export function decorate<TParams extends unknown[], TResult>(
  func: (...params: TParams) => Promise<TResult>,
  ...params: TParams
): () => Promise<TResult> {
  return () => {
    // ...
    return func(...params);
  };
}

type ArbitraryData = {
  property: string;
};

export function getArbitraryData(_id: number): Promise<ArbitraryData[]> {
  return Promise.resolve([{property: '123'}]);
}

deferQuery({
  queryFn: decorate(getArbitraryData, 10),
  onSuccess(data) {
    //      ^? (parameter) data: unknown
    data.forEach(console.log);
  },
});

const getData = decorate(getArbitraryData, 10);
deferQuery({
  queryFn: getData,
  onSuccess(data) {
    //      ^? (parameter) data: ArbitraryData[]
    data.forEach(console.log);
  },
});

🙁 Actual behavior

data is typed as ArbitraryData[] only when the decorated method is stored in a variable.

🙂 Expected behavior

data is typed as ArbitraryData[] in both cases.

deferQuery({
  queryFn: decorate(getArbitraryData, 10),
  onSuccess(data) {
    //      ^? (parameter) data: ArbitraryData[]
    data.forEach(console.log);
  },
});

const getData = decorate(getArbitraryData, 10);
deferQuery({
  queryFn: getData,
  onSuccess(data) {
    //      ^? (parameter) data: ArbitraryData[]
    data.forEach(console.log);
  },
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions