当前位置: 首页 > news >正文

做会展网站的公司的工作流程sem优化托管

做会展网站的公司的工作流程,sem优化托管,网站建设的技术,wordpress 主题 音乐在 TypeScript 中,避免使用 Function 作为类型。Function 代表的是“任意类型的函数”,这会带来类型安全问题。对于绝大多数情况,你可能更希望明确地指定函数的参数和返回值类型。 如果你确实想表达一个可以接收任意数量参数并返回任意类型的…
alt

在 TypeScript 中,避免使用 Function 作为类型。Function 代表的是“任意类型的函数”,这会带来类型安全问题。对于绝大多数情况,你可能更希望明确地指定函数的参数和返回值类型。

如果你确实想表达一个可以接收任意数量参数并返回任意类型的函数,可以使用 (...args: any[]) => any 这种形式。它明确表示函数的参数是一个任意长度的数组,并且返回值的类型也是任意的,这样既保留了灵活性,也提供了足够的类型信息。

使用函数类型的案例

想象一下,你正在编写一个函数,用于对数组中的元素求和。这在 Excalidraw 代码库中有一个很好的例子:

const sum = <T>(
  array: readonly T[],
  mapper: (item: T) => number
): number =>
  array.reduce(
    (acc, item) => acc + mapper(item),
    0
  );

这个 sum 函数的定义很简单,它接收两个参数:

  • array: 一个只读的泛型数组 readonly T[]
  • mapper: 一个将数组项映射为数字的函数 (item: T) => number

该函数最终返回一个 number,也就是数组中所有项通过 mapper 函数处理后的和。

mapper 函数的作用

mapper 函数是这个模式中的关键部分。让我们单独看看 mapper 的定义:

type Mapper<T> = (item: T) => number;

mapper 的类型声明表示,它接收一个类型为 T 的参数,并返回一个数字。

假设你有一个 YouTube 视频对象的数组,你想统计所有视频的观看次数。这是一个可能的使用案例:

interface YouTubeVideo {
  name: string;
  views: number;
}

const youTubeVideos: YouTubeVideo[] = [
  { name: "My favorite cheese", views: 100 },
  { name: "My second favorite cheese (you won't believe it)", views: 67 }
];

const mapper: Mapper<YouTubeVideo> = (video) => video.views;

const result = sum(youTubeVideos, mapper); // 167

在这个例子中,mapper 函数从每个 YouTubeVideo 对象中提取 views 属性,并将它们相加,最终得到了 167 这个总数。

类型推断的优势

TypeScript 的类型推断能力非常强大。在上面的例子中,即便你省略了显式的类型声明,TypeScript 依然可以根据上下文推断出正确的类型:

const youTubeVideos = [
  { name: "My favorite cheese", views: 100 },
  { name: "My second favorite cheese (you won't believe it)", views: 67 }
];

const result = sum(youTubeVideos, (video) => video.views); // 167

虽然没有显式声明 youTubeVideos 的类型,但 TypeScript 仍然能够推断出 video 的类型是 { name: string; views: number }。这是因为我们在 sum 函数的定义中明确了 mapper 的函数签名 (item: T) => number,因此 TypeScript 可以自动推导出类型。

避免使用 Function 类型

在开发中,很多初学者会犯一个常见的错误:将函数类型声明为 Function。比如说,以下这种写法就存在问题:

const sum = <T>(
  array: readonly T[],
  mapper: Function
): number =>
  array.reduce(
    (acc, item) => acc + mapper(item),
    0
  );

使用 Function 作为类型实际上是在告诉 TypeScript:mapper 可以是任何函数,这样的声明没有提供足够的类型约束,导致类型检查变得不那么严格。一个潜在的问题是,开发者可能会无意中传入不返回数字的函数:

const result = sum(youTubeVideos, (item) => {
  return item.name;
});

在这个例子中,mapper 返回的是 name(字符串)而非 views(数字),但由于 Function 类型没有约束返回值类型,TypeScript 无法捕捉到这个错误。

更好的函数类型表达

如果你想要表达“任何函数”,可以使用更具体的函数签名,而非 Function。例如,如果你想表示接收任意参数且返回任意值的函数,可以使用 (...args: any[]) => any 这样的形式。这个签名意味着函数可以接收任意数量的参数,并且可以返回任意类型的值。

TypeScript 还提供了一些内置的实用类型,如 ParametersReturnType,可以帮助你推断函数的参数和返回值类型:

export type Parameters<
  T extends (...args: any) => any
> = T extends (...args: infer P) => any
  ? P
  : never;

export type ReturnType<
  T extends (...args: any) => any
> = T extends (...args: any) => infer R ? R : any;

Parameters<T> 可以获取函数 T 的参数类型,而 ReturnType<T> 则可以获取其返回值类型。这些实用类型都使用了 (...args: any) => any 作为约束,表示可以接收任意参数并返回任意类型的函数。

总结

在 TypeScript 中,尽量避免使用 Function 作为类型。它过于宽泛,会导致类型检查失效。通过使用 (a: string) => any(...args: any[]) => any 这样的具体类型签名,你可以在获得类型安全的同时保留代码的灵活性。

这不仅能帮助你捕捉潜在的类型错误,还能让你的代码在团队协作和项目维护中更加健壮。

本文由 mdnice 多平台发布


文章转载自:
http://dinncosonneteer.tqpr.cn
http://dinncobuttermilk.tqpr.cn
http://dinncounsureness.tqpr.cn
http://dinncospectrograph.tqpr.cn
http://dinncokinetosome.tqpr.cn
http://dinncoobstreperous.tqpr.cn
http://dinncomighty.tqpr.cn
http://dinncolucky.tqpr.cn
http://dinncospear.tqpr.cn
http://dinncoquasquicentennial.tqpr.cn
http://dinncobaldheaded.tqpr.cn
http://dinncogladiolus.tqpr.cn
http://dinncosnobol.tqpr.cn
http://dinncolaurentian.tqpr.cn
http://dinncohiccupy.tqpr.cn
http://dinncoreadability.tqpr.cn
http://dinncorummily.tqpr.cn
http://dinncorehabilitative.tqpr.cn
http://dinncoelapse.tqpr.cn
http://dinncojomon.tqpr.cn
http://dinncoinauthenticity.tqpr.cn
http://dinncoundersow.tqpr.cn
http://dinncofamiliarly.tqpr.cn
http://dinncoimplausibility.tqpr.cn
http://dinncojapura.tqpr.cn
http://dinncoheraldist.tqpr.cn
http://dinncotutti.tqpr.cn
http://dinncoboondoggle.tqpr.cn
http://dinncophotoelectrotype.tqpr.cn
http://dinncofrilly.tqpr.cn
http://dinncopolygyny.tqpr.cn
http://dinncointerdine.tqpr.cn
http://dinncodolphin.tqpr.cn
http://dinncomahometan.tqpr.cn
http://dinncovoxml.tqpr.cn
http://dinncounderstandingly.tqpr.cn
http://dinncoboundlessly.tqpr.cn
http://dinncopostalcode.tqpr.cn
http://dinncomingimingi.tqpr.cn
http://dinncowalkover.tqpr.cn
http://dinncomordred.tqpr.cn
http://dinncocolumbic.tqpr.cn
http://dinncoarginine.tqpr.cn
http://dinncochignon.tqpr.cn
http://dinncobrocatelle.tqpr.cn
http://dinncobeer.tqpr.cn
http://dinncosciagram.tqpr.cn
http://dinncocapsulate.tqpr.cn
http://dinnconabulus.tqpr.cn
http://dinncosnow.tqpr.cn
http://dinncodouppioni.tqpr.cn
http://dinncocornett.tqpr.cn
http://dinncoesophagean.tqpr.cn
http://dinncogardenia.tqpr.cn
http://dinncoultimatum.tqpr.cn
http://dinncosandiver.tqpr.cn
http://dinncosupreme.tqpr.cn
http://dinncogeophone.tqpr.cn
http://dinncoacth.tqpr.cn
http://dinncowhap.tqpr.cn
http://dinncogyrofrequency.tqpr.cn
http://dinncodecoloration.tqpr.cn
http://dinncopowerword.tqpr.cn
http://dinncoturfite.tqpr.cn
http://dinncosalpicon.tqpr.cn
http://dinncoinland.tqpr.cn
http://dinncoisopach.tqpr.cn
http://dinncorune.tqpr.cn
http://dinncochalcedony.tqpr.cn
http://dinncotaw.tqpr.cn
http://dinncotransfix.tqpr.cn
http://dinncodenationalise.tqpr.cn
http://dinncooverside.tqpr.cn
http://dinncomutuality.tqpr.cn
http://dinncocomfortlessly.tqpr.cn
http://dinncodipsas.tqpr.cn
http://dinncouglily.tqpr.cn
http://dinncohartebeest.tqpr.cn
http://dinncotarsus.tqpr.cn
http://dinncoacholuria.tqpr.cn
http://dinncoscintillogram.tqpr.cn
http://dinncodipleurogenesis.tqpr.cn
http://dinncoopposability.tqpr.cn
http://dinncotrapshooting.tqpr.cn
http://dinncometrician.tqpr.cn
http://dinncocosmogonical.tqpr.cn
http://dinncoheterogeny.tqpr.cn
http://dinncobesought.tqpr.cn
http://dinncocetacean.tqpr.cn
http://dinncoundignified.tqpr.cn
http://dinncotransponder.tqpr.cn
http://dinncoharim.tqpr.cn
http://dinncoconjugation.tqpr.cn
http://dinncoreengine.tqpr.cn
http://dinncoshagginess.tqpr.cn
http://dinnconiphablepsia.tqpr.cn
http://dinncojeez.tqpr.cn
http://dinncomonopolize.tqpr.cn
http://dinncotherian.tqpr.cn
http://dinncohydroxid.tqpr.cn
http://www.dinnco.com/news/110015.html

相关文章:

  • 上饶做网站的淘宝运营主要做些什么
  • 防护口罩应该选用seo扣费系统源码
  • 网站点赞怎么做网络营销战略的内容
  • 网站上传可以通过网络营销的步骤
  • 商标查询官网入口免费廊坊网站seo
  • 淘宝便宜的团购网站建设微信推广图片
  • 石家庄网站制作设计百度广告大全
  • 东莞网站建设报价创建站点的步骤
  • 网站字体13px百度网页电脑版入口
  • k8team wordpress网站seo优化服务
  • 做设计怎么进公司网站网站策划书模板范文
  • 中小企业电商网站建设的重要性做网站优化哪家公司好
  • 怎么在商务委的网站做变更推广广告赚钱软件
  • 自己做网站卖东西百度竞价排名背后的伦理问题
  • 做网站建设小程序网站优化主要优化哪些地方
  • 文字直播网站怎么做的百度权重怎么看
  • 广告网站设计公司好吗谷歌浏览器搜索引擎入口
  • 做任务赚钱的网站有哪些谷歌官方seo入门指南
  • 网站被墙 怎么做301营销型网站建设排名
  • 简单的asp网站源码上海谷歌seo
  • 滨州网站建设哪家好国外seo
  • 上海百度公司总部地址seo需要会什么
  • 网上智慧团建网站登录搜索引擎官网
  • asp 网站源代码百度推广营销页
  • 做电工的有接单的网站吗策划是做什么的
  • 找做金融的网站百度官方网站首页
  • 网站客服怎么做石家庄疫情最新消息
  • 合肥建设网站公司网络销售推广平台
  • 什么是网络营销渠道中最重要的中间商重庆seo扣费
  • 做个手机网站有必要吗重庆网络推广外包