- 31
- 0
刚开始用 typescript 有些不懂 请教大家下.
用接口去定义匿名函数 且 参数不为 结构赋值 这样能够准确校验类型
type NoticeType = 'error' | 'success' | 'info' | 'warning'
interface MeaageFunc {
(message: string, type: NoticeType, title: string, duration: number): void
}
let message: MeaageFunc
message = function(message,type = 'info', title = '提示', duration = 5000) {}
问题1
如果我用 结构赋值 下面的写法 不会校验参数 type 的类型 怎么修改
type NoticeType = 'error' | 'success' | 'info' | 'warning'
interface MessageConfig {
message: string
type?: NoticeType
title?: string
duration?: number
}
interface MeaageFunc {
({message, type, title, duration}: MessageConfig): void
}
let message: MeaageFunc
message = function({message,type = 'infosss', title = '提示', duration = 5000}) {}
问题2 如果我定义一个接口 里面有多个具名函数 下面的写法不能准确的参数 type 的校验 要怎么修改呢
interface NoticeConfig {
message:({message, type, title, duration}: MessageConfig) => void
notify:({message, type, title, duration}: MessageConfig) => void
}
const Notice: NoticeConfig = {
message({message,type = 'infoasas', title = '提示', duration = 5000}) {}
} - 共 0 条
- 全部回答
-
︶ㄣ随гゞ枫 普通会员 1楼
在 TypeScript 中,接口是一个包含一组类型和方法的蓝图,它定义了函数的类型和行为。接口可以用于表示类的抽象,帮助你定义可复用的代码块。
例如,假设你有一个名为
Person的接口,它有以下方法:typescript interface Person { name: string; age: number; }然后,你可以定义一个名为
PersonImpl的类,它实现了Person接口:```typescript class PersonImpl implements Person { name: string; age: number;
constructor(name: string, age: number) { this.name = name; this.age = age; }
getAge(): number { return this.age; }
setName(name: string): void { this.name = name; } } ```
在这个例子中,
PersonImpl类实现了Person接口的所有方法,包括getAge和setName。你可以像使用任何其他类一样使用它,只需要确保它实现了Person接口。例如,你可以创建一个新的
PersonImpl实例,并设置它的名字和年龄:typescript const person = new PersonImpl("Alice", 25); console.log(person.getName()); // 输出 "Alice" person.setName("Bob"); console.log(person.getName()); // 输出 "Bob"这就是如何在 TypeScript 中定义接口和函数。
- 扫一扫访问手机版
回答动态

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器更新之后。服务器里面有部分玩家要重新创建角色是怎么回事啊?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题函数计算不同地域的是不能用内网吧?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题ARMS可以创建多个应用嘛?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题在ARMS如何申请加入公测呀?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题前端小程序接入这个arms具体是如何接入监控的,这个init方法在哪里进行添加?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器刚到期,是不是就不能再导出存档了呢?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器的游戏版本不兼容 尝试更新怎么解决?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器服务器升级以后 就链接不上了,怎么办?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器转移以后服务器进不去了,怎么解决?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器修改参数后游戏进入不了,是什么情况?预计能赚取 0积分收益
- 回到顶部
- 回到顶部

