首页 > 其他分享 >[Typescript] Type incompatible function argument as never

[Typescript] Type incompatible function argument as never

时间:2024-08-07 17:55:32浏览次数:7  
标签:function Typescript const string incompatible never number boolean input

For the following code:

const objOfFunctions = {
  string: (input: string) => input.toUpperCase(),
  number: (input: number) => input.toFixed(2),
  boolean: (input: boolean) => (input ? "true" : "false"),
};

const format = (input: string | number | boolean) => {
  const inputType = typeof input as "string" | "number" | "boolean";
  const formatter = objOfFunctions[inputType];

  return formatter(input); // Error: Argument of type 'string | number | boolean' is not assignable to parameter of type 'never'.
  // Type 'string' is not assignable to type 'never'.
};

 

This is due to inputis typed as intersection of string & number & booleanwhich results as never

So you have to do:

const format = (input: string | number | boolean) => {
  const inputType = typeof input as "string" | "number" | "boolean";
  const formatter = objOfFunctions[inputType];

  return formatter(input as never);
};

 

标签:function,Typescript,const,string,incompatible,never,number,boolean,input
From: https://www.cnblogs.com/Answer1215/p/18347562

相关文章

  • [Typescript] Typing Functions with Object Params
    import{expect,it,vitest}from'vitest';constlogId=(obj:{id:string})=>{console.log(obj.id);};constlogName=(obj:{name:string})=>{console.log(obj.name);};constloggers=[logId,logName];constlogAll=(o......
  • [Typescript] Understanding TypeScript's Function Parameter Comparisons
    Makethosepass:import{Equal,Expect}from"@total-typescript/helpers";typeEvent="click"|"hover"|"scroll";typeCallbackType=unknown;constlistenToEvent=(callback:CallbackType)=>{};listen......
  • Function Calling + LangChain 拉通业务系统的技术架构
    近年来,大型语言模型(LLMs)如GPT-4的发展极大地推动了自然语言处理(NLP)领域的进步。这些模型在内容生成、语言翻译和对话系统等多个应用中展示了其强大的能力。然而,传统语言模型的局限性在于它们只能进行语言生成,无法与外部系统、API或自定义函数进行交互。本文将介绍如何通过Fu......
  • kotlin 与java 接口不兼容@FunctionalInterface
    需求:获取当前方法名为了获取当前方法名已知的有1.堆栈获取2.通过classs的enclosingMethod.name比较准确的是enclosingMethod,但是为了获取这个还需要一个内部类,于是用到了Runable,但是run方法没有返回值于是出现了神奇问题1.照Runnable抄一份:源码如下:@FunctionalInterfacepu......
  • BIOS1101 Evolutionary and Functional
    BIOS1101Evolutionaryand Functional Biology -2024GeneralCourseInformationCourseCode:  BIOS1101Year : 2024Term: Term2CourseDetails&OutcomesCourseDescriptionThis course examines the evolutionary history of life on earth fr......
  • 【TS】 TypeScript声明文件:打通JavaScript和TypeScript的桥梁
     TypeScript声明文件的讲解: TypeScript声明文件(DeclarationFile)在TypeScript项目中具有举足轻重的地位,它是连接TypeScript严格的类型系统与外部无类型或类型不明确的JavaScript代码的关键纽带。 声明文件的核心价值在于为TypeScript编译器提供必要的类型信息......
  • OpenAI Function Call大模型调用单个多个agent案例
    参考:https://platform.deepseek.com/api-docs/zh-cn/function_callinghttps://blog.csdn.net/qq_31095905/article/details/139143778https://blog.csdn.net/jacbo/article/details/136278619##官方案例https://cookbook.openai.com/examples/how_to_call_functions_wi......
  • TypeScript 基础类型与类型声明
    前言在JavaScript中,变量是没有类型的,变量的值的类型是在运行时确定的,这被称为动态类型。这意味着可以在不同的时间将不同类型的值赋给同一个变量,并且JavaScript会在运行时根据当前赋给变量的值来确定其类型。示例:leta;//声明一个变量aa=10;//此时a的......
  • TypeScript 类型断言、类型推论
    类型断言类型断言是一种TypeScript特性,用于告诉编译器将一个值视为特定的类型,即使编译器本身的类型推断可能不同。类型断言并不会改变变量的实际运行时类型,而是在编译阶段告知TypeScript编译器开发者期望该表达式具有某种类型。注意:类型断言不是类型转换,因为转换通常......
  • SDN(Software-Defined Networking,软件定义网络),NFV(Network Functions Virtualization,网
    目录SDN(Software-DefinedNetworking,软件定义网络)NFV(NetworkFunctionsVirtualization,网络功能虚拟化)SDN(软件定义网络)NFV(网络功能虚拟化)SDN的优势NFV的优势DC(数据中心)网关与MEC(移动边缘计算)节点DC网关MEC节点DC网关与MEC节点的协同作用SDN(Software-DefinedNet......