首页 > 其他分享 >type关键字

type关键字

时间:2023-08-21 19:22:04浏览次数:29  
标签:string handleEvent 关键字 EventNames type Name

1.类型别名

作用:类型别名用来给一个类型起个新名字。

type关键字用法如下:

type Name = string;
type NameResolver = () => string;
type NameOrResolver = Name | NameResolver;
function getName(n: NameOrResolver): Name {
    if (typeof n === 'string') {
        return n;
    } else {
        return n();
    }
}

备注:类型别名常用于联合类型。

2.字符串字面量类型

作用:用来约束取值只能是某几个字符串中的一个。

type关键字用法如下:

type EventNames = 'click' | 'scroll' | 'mousemove';
function handleEvent(ele: Element, event: EventNames) {
    // do something
}

handleEvent(document.getElementById('hello'), 'scroll');  // 没问题
handleEvent(document.getElementById('world'), 'dblclick'); // 报错,event 不能为 'dblclick'

// index.ts(7,47): error TS2345: Argument of type '"dblclick"' is not assignable to parameter of type 'EventNames'.  

本文参考文档:https://ts.xcatliu.com

 

标签:string,handleEvent,关键字,EventNames,type,Name
From: https://www.cnblogs.com/lijingru/p/17646835.html

相关文章

  • TypeScript使用技巧
    文章目录使用技巧TypeScript内置的工具类型keyofextends限定泛型interface与type区别TypeScript作为JavaScript的超集,通过提供静态类型系统和对ES6+新特性的支持,使JavaScript开发变得更加高效和可维护。掌握TypeScript的使用技巧,可以帮助我们更好地开发和组织JavaScript项......
  • 采用typescript编写,实现ofd前端预览、验章。
    前言 浏览器内核已支持pdf文件的渲染,这极大的方便了pdf文件的阅读和推广。ofd文件作为国产板式标准,急需一套在浏览器中渲染方案。本人研究ofd多年,分别采用qt、c#开发了ofd阅读器。本人非前端开发人员,对js、typescript并不熟悉,所以对开发web版阅读器迟迟没有下手。但是,本人......
  • 【待解决】Typed variable declaration : Class: Workbook not found in namespace
    需求接口需要导入excel,自动生成excel文件以及数据问题idea中可以成功依赖<!--https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml--><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxm......
  • 关于spring中的TypeDescriptor、ResolvableType功能说明
    TypeDescriptor:类型描述符该类位于org.springframework.core.convert包下,convert包主要用于类型转换相关功能,在类型转换过程中,通常需要从一个类型转为另一个类型,而Java类型包含基本类型,数组、集合、泛型以及普通类型,该类试图将这些不同的类型使用一种统一的类型来表示,因此方法中提......
  • [React Typescript] React namespace
    export=React;exportasnamespaceReact;declarenamespaceReact{////ReactElements//----------------------------------------------------------------------typeElementType<P=any>={[KinkeyofJSX.I......
  • [React Typescript] Inferring Type Arguments in Curried Hooks
    import{DependencyList,useMemo,useState}from"react";import{Equal,Expect}from"../helpers/type-utils";constuseCustomState=<TValue>(initial:TValue)=>{const[value,set]=useState<TValue>(initial);......
  • [React Typescript] Function overload in React hook
    import{useState}from"react";import{Equal,Expect}from"../helpers/type-utils";typeUseStateReturnValue<T>={value:T;set:React.Dispatch<React.SetStateAction<T>>;};exportfunctionuseStateAsObjec......
  • mysql 关键字 保留字
     MySQL::MySQL8.0ReferenceManual::9.3KeywordsandReservedWordshttps://dev.mysql.com/doc/refman/8.0/en/keywords.html9.3 KeywordsandReservedWordsKeywordsarewordsthathavesignificanceinSQL.Certainkeywords,suchas SELECT, DELET......
  • ffpyplayer源码编译报错:ffpyplayer/tools.pyx:182:28: Cannot assign type 'void (*)(
    编译ffpyplayer报错,具体错误如标题。  报错信息:ffpyplayer/tools.pyx:182:28:Cannotassigntype'void(*)(void*,int,constchar*,va_list)except*nogil'to'void(*)(void*,int,constchar*,va_list)noexceptnogil'  解决方法:pipinstallblos......
  • C++ 隐式转换与explicit关键字
    隐式转换与explicit关键字隐式转换函数构造的隐式转换,直接上代码:#include<bits/stdc++.h>classEntity{private: std::stringm_Name; intm_Age;public: Entity(conststd::string&name) :m_Name(name),m_Age(-1){} Entity(intage) :m_Name("Unknown"),m_A......