简体   繁体   English

Typescript 函数根据可选参数返回类型

[英]Typescript function return type based on optional arguments

How can bad types be resolved?如何解决坏类型?

TS sandbox. TS 沙箱。

interface X<T> {
    abc:T
}

declare function deepLock<Obj extends Record<string, any>,>(obj: Obj): X<Obj>;
declare function deepLock<Obj extends Record<string, any>,>(obj: Obj, options: {}): X<Obj>;
declare function deepLock<
  Obj extends Record<string, any>,
  Action extends "freeze" | "seal" | "preventExtensions",
  Options extends { action?: Action }
>(obj: Obj, options?: Options): Action extends 'freeze' ? X<Obj> : Obj

// ok
const x = deepLock({}) // expect X<{}>
const x2 = deepLock({}, {}) // expect X<{}>

// need to be fixed
const x3 = deepLock({}, {action: 'freeze'}) // expect X<{}>
const x4 = deepLock({}, {action: 'seal'}) // expect Obj
const x5 = deepLock({}, {action: 'preventExtensions'}) // expect Obj

So you just need to adjust your function definition very slightly to:所以你只需要稍微调整你的函数定义:

declare function deepLock<
  Obj extends Record<string, any>,
  Action extends "seal" | "preventExtensions" | "freeze",
>(obj: Obj, options?: { action?: Action }): 'freeze' extends Action ? X<Obj> : Obj;

The problem you are facing is that Action is extending "seal" | "preventExtensions" | "freeze"您面临的问题是Action正在扩展"seal" | "preventExtensions" | "freeze" "seal" | "preventExtensions" | "freeze" "seal" | "preventExtensions" | "freeze" and so by definition Action extends "freeze" will always be true as it always extends that. "seal" | "preventExtensions" | "freeze" ,因此根据定义, Action extends "freeze"将始终为真,因为它总是扩展它。

So in order to correctly narrow the types you have to do "freeze" extends Action which will only ever be true for one of the three possibilities for Action.因此,为了正确缩小类型,您必须执行"freeze" extends Action ,这仅适用于 Action 的三种可能性之一。

Playground link 游乐场链接

Hope that makes sense希望这是有道理的

暂无
暂无

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

相关问题 基于可选参数属性函数的打字稿方法返回类型 - Typescript method return type based on optional param property function 如何基于参数中的函数返回类型指定打字稿条件类型 - How to specify typescript conditional type based on a function return type in the arguments TypeScript 根据类型参数返回不同的类型 - TypeScript return different type based on type arguments 我如何让 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 Typescript:基于 arguments 道具的条件返回类型 - Typescript: Conditional return type based on arguments props Typescript 基于arguments的动态联合返回类型 - Typescript dynamic union return type based on arguments 在TypeScript中,如何根据其参数之一的返回类型来确定函数的返回类型? - In TypeScript, how do I make a function's return type based on the return type of one of its arguments? Typescript function 返回类型基于可选参数存在而不使用 function 重载 - Typescript function return type based on optional parameter presence without using function overloads 打字稿:只键入函数的参数或返回类型 - Typescript: typing only the arguments or the return type of a function Typescript仅复制函数参数而不返回类型 - Typescript copying only function arguments and not return type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM