简体   繁体   English

在使用通用初始值设定项 function 时,如何让 typescript 正确推断类型?

[英]How do I have typescript have the type properly infered when using a generic initializer function?

I have a function that may take a parameter in the form of a callable like this:我有一个 function 可以采用这样的可调用形式的参数:

type Initial<T> = T | PromiseLike<T> | (() => T) | (() => PromiseLike<T>)

const usePromise = <T>(initial: Initial<T>): T => {
  let result: unknown = null
  
  /* ... */
  
  return result as T
}

If the type of initial is something like Promise<number> or PromiseLike<number> , typescript correctly infers the type of T as number .如果initial的类型类似于Promise<number>PromiseLike<number> ,则 typescript 会正确地将T的类型推断为number However, if initial is of a function type like () => PromiseLike<number> , typescript infers T as PromiseLike<number> .但是,如果initial是 function 类型,如() => PromiseLike<number> ,则 typescript 将T推断为PromiseLike<number> For example:例如:

// good:
const initial1 = new Promise<number>(() => null)
const result1 = usePromise(initial1)  // result1: number (OK)

// bad:
const initial2: () => PromiseLike<number> = () => new Promise(() => null)
const result2 = usePromise(initial2)  // result2: PromiseLike<number> (ERROR, should be number)

Note: obviously these promises would never resolve nor reject, but this question is purely about the type inference.注意:显然这些承诺永远不会解决或拒绝,但这个问题纯粹是关于类型推断的。

How do I get typescript to also correctly infer the type in the second example?如何让 typescript 也正确推断第二个示例中的类型?

The union presents multiple valid inference targets.该联合提供了多个有效的推理目标。 The singular T type is allways valid and TypeScript propably does not put in the computation effort to see that (() => PromiseLike<T>) is also a valid target.单数T类型始终有效,并且 TypeScript 可能不会进行计算以查看(() => PromiseLike<T>)也是一个有效目标。 We need to explicitly give an inference order to make the compiler examine more specific types like (() => PromiseLike<T>) first.我们需要明确给出一个推理顺序,让编译器首先检查更具体的类型,例如(() => PromiseLike<T>) I would suggest the use of overloads.我建议使用重载。

The overloads are resolved in order.重载按顺序解决。 The first overload is examined before the second one etc...第一个过载在第二个过载之前被检查等等......

type Initial<T> = T | PromiseLike<T> | (() => T) | (() => PromiseLike<T>)

function usePromise<T>(initial: (() => PromiseLike<T>)): T
function usePromise<T>(initial: (() => T)): T
function usePromise<T>(initial: PromiseLike<T>): T
function usePromise<T>(initial: T): T
function usePromise<T>(initial: Initial<T>): T {
  let result: unknown = null
  
  /* ... */
  
  return result as T
}

Playground 操场

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 打字稿多个泛型无法正确推断 - typescript multiple generic cannot infered properly 在 Typescript 中,如何允许类型具有指向该类型递归值的泛型的所有键? - In Typescript how do I allow a type to have all keys of a generic that point to a recursive value of that type? TypeScript/React - 如何让 function 接受通用组件类型? - TypeScript/React - How to have function accept a generic component type? 在指定其他类型时,如何让 TypeScript 推断受限泛型类型的值? - How can I have TypeScript infer the value for a constrained generic type when specifying additional types? 当我想使用文件缓冲区调用函数时,我必须使用哪种打字稿类型? - Which typescript type do I have to use when I want to call a function with file buffer? 类型文字属性在TypeScript中不能有初始值设定项 - A type literal property cannot have an initializer in TypeScript TypeScript:如何正确键入此 `groupBy` function - TypeScript: How do I properly type this `groupBy` function 如何在 TypeScript 中使用具有派生参数类型的通用 args 变量调用 function? - How do I call a function using a generic args variable with a derived Parameters type in TypeScript? 打字稿的类型有时会解析为函数返回类型 - Typescript infered type sometimes resolved to any on function return type 如何在打字稿中的接口中指定回调,而不必为其命名类型 - How do I specify a callback in an interface in typescript but not have to name a type for it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM