简体   繁体   English

如何键入函数的 arguments 但保留返回类型“推断”?

[英]How do I type a function's arguments but leave the return type "inferred"?

Below I have two functions originalhelloWorld which is untyped and helloWorld which has a type.下面我有两个函数originalhelloWorld是无类型的, helloWorld是有类型的。 You can see that the return of type o returns the "inferred" return type (what is the name for this), and type x returns "any".可以看到type o的return返回的是“推断的”返回类型(这个叫什么名字),type x返回的是“any”。

How can I have the ExampleFunction type the functions arguments but leave the return type inferred?如何让ExampleFunction键入函数 arguments 但保留推断的返回类型? I've tried several combinations of generics, and nothing seems to work.我已经尝试了 generics 的几种组合,但似乎没有任何效果。

Typescript Playground Typescript 游乐场

const originalhelloWorld = (greeting: string | boolean) => {
   if (typeof greeting === 'boolean') return greeting
   return `hello ${greeting}`
}

type o = ReturnType<typeof originalhelloWorld>
//  ^? type o = string | boolean

/* ------------------------------------ */

type ExampleFunction = (greeting: string | boolean) => any

const helloWorld: ExampleFunction = (greeting) => {
   if (typeof greeting === 'boolean') return greeting
   return `hello ${greeting}`
}

type x = ReturnType<typeof helloWorld>
//  ^? type x = any

The new satisfies operator in typescript 4.9 works: typescript 4.9 工程中的新satisfies运算符:

Playground Link: 游乐场链接:

const originalhelloWorld = (greeting: string | boolean) => {
   if (typeof greeting === 'boolean') return greeting
   return `hello ${greeting}`
}

type o = ReturnType<typeof originalhelloWorld>
//  ^?

/* ------------------------------------ */

type ExampleFunction = (greeting: string | boolean) => any

const helloWorld = ((greeting) => {
   if (typeof greeting === 'boolean') return greeting
   return `hello ${greeting}`
}) satisfies ExampleFunction

type x = ReturnType<typeof helloWorld>
//  ^?

暂无
暂无

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

相关问题 TypeChecker API:如何找到推断类型 arguments 到 function? - TypeChecker API: How do I find inferred type arguments to a function? 在TypeScript中,如何键入函数的参数而不是返回值? - In TypeScript, how do i type a function's arguments but not the return value? 如何创建一个泛型 typescript 类型 function 并从参数类型推断出返回类型? - How do I make a generic typescript type function that has the return type inferred from the argument type? 在TypeScript中,如何根据其参数之一的返回类型来确定函数的返回类型? - In TypeScript, how do I make a function's return type based on the return type of one of its arguments? 在类型为联合的函数中未正确推断参数和返回类型 - Arguments and return type not inferred properly in function typed as union 我如何让 typescript 根据 function2E977777777777777777777777777777777777777777777777777777777777777777777777777777777777BED18 的返回类型 functionC17A94F14Z 的返回类型来推断 function 的返回类型 - How do I get typescript to infer the return type of a function based on the return type on a function of its arguments 为什么泛型类型参数 T 在返回类型中是“未知的”? 如何键入此 function 以便正确推断返回类型? - Why is the generic type parameter T “unknown” in the return type? How can I type this function so that the return type is correctly inferred? 将联合类型参数传递给 function 时,如何键入接收函数的 arguments? - When passing a union type argument to a function, how do I type the receiving function's arguments? 如何修复通用 TypeScript function 以返回有效的推断类型? - How to fix generic TypeScript function to return valid inferred type? 未在 TypeScript 上推断的函数的返回类型 - Return type from function not inferred on TypeScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM