10 个关于 TypeScript 的小技巧
web前端开发
共 5448字,需浏览 11分钟
· 2020-09-12
英文 | https://www.sangle7.com/
1、 TypeScript 和 DOM
const textEl = document.querySelector('inpot');
console.log(textEl.value);
// ? Property 'value' does not exist on type 'Element'.
interface HTMLElementTagNameMap {
a: HTMLAnchorElement;
abbr: HTMLElement;
address: HTMLElement;
applet: HTMLAppletElement;
area: HTMLAreaElement;
article: HTMLElement;
/* ... */
input: HTMLInputElement;
/* ... */
}
textEl.addEventListener('click', (e) => {
console.log(e.clientX);
});
2、期望泛型
document.querySelector('input.action')
textEl = document.querySelector < HTMLInputElement > 'input.action';
console.log(textEl.value);
// ? 'value' is available because we've instructed TS
// about the type the 'querySelector' function works with.
3、“我们真的找到了吗?”
{
"compilerOptions": {
"strictNullChecks": true
}
}
const textEl = document.querySelector('input');
console.log(textEl!.value);
// ? with "!" we assure TypeScript
// that 'textEl' is not null/undefined
5、当迁移到 TS…
// tslint:disable
let mything = 2;
mything = 'hi';
// ? Type '"hi"' is not assignable to type 'number'
mything = 'hi' as any;
// ? if you say "any", TypeScript says ¯\_(ツ)_/¯
6、更多限制
function fn(param) {
console.log(param);
}
{
"compilerOptions": {
"noImplicitAny": true
}
}
{
"rules": {
"typedef": [
true,
"call-signature"
]
}
}
7、类型保护
type BookId = number | string;
function returnFormatterId(id: BookId) {
return id.toUpperCase();
// ? 'toUpperCase' does not exist on type 'number'.
}
function returnFormatterId(id: BookId) {
if (typeof id === 'string') {
// we've made sure it's a string:
return id.toUpperCase(); // so it's ?
}
// ? TS also understands that it
// has to be a number here:
return id.toFixed(2)
}
8、再谈泛型
interface Bookmark {
id: string;
}
class BookmarksService {
items: Bookmark[] = [];
}
interface Movie {
id: string;
name: string;
}
class SearchPageComponent {
movie: Movie;
constructor(private bs: BookmarksService) {}
getFirstMovie() {
// ? types are not assignable
this.movie = this.bs.items[0];
// ? so you have to manually assert type:
this.movie = this.bs.items[0] as Movie;
}
getSecondMovie() {
this.movie = this.bs.items[1] as Movie;
}
}
class BookmarksService
{ items: T[] = [];
}
class BookmarksService
{ items: T[] = [];
}
class SearchPageComponent {
movie: Movie;
constructor(private bs: BookmarksService
) {} getFirstMovie() {
this.movie = this.bs.items[0]; // ?
}
getSecondMovie() {
this.movie = this.bs.items[1]; // ?
}
}
class BookmarksService
{ items: T[] = [];
}
const bs = new BookmarksService();
// I don't have to provide the type for the generic now
// - in that case 'bs' will be of that default type 'Bookmark'
9、路由参数
export interface DashboardRouteParams {
countryId: string;
deviceId: string;
}
const routes: Routes = [{
path: 'country/:countryId/device/:deviceId/dashboard'
}]
10、根据 API 响应创建 Interface
http://www.json2ts.com http://www.jsontots.com
https://app.quicktype.io/
评论
真高!比亚迪员工爆料比亚迪在越南的薪资水平:基本工资480万,全勤奖35万,交通补助20万,餐补110万,每周6天,每天10小时
上一篇:某大公司为逼迫员工离职,竟然把他的工位安排到厕所旁,没想到他直接开始记录领导的如厕时间,还发到公司大群...对此,你怎么看?--完--PS:欢迎在留言区留下你的观点,一起讨论提高。如果今天的文章让你有新的启发,欢迎转发分享给更多人。全文完,感谢你的耐心阅读。如果你还想看到我的文章,请一定给本
开发者全社区
0
某大公司为逼迫员工离职,竟然把他的工位安排到厕所旁,没想到他直接开始记录领导的如厕时间,还发到公司大群...
上一篇:字节的跳动职级与薪资(2024年)我们与公司间的合作,宛如两艘船只在茫茫大海上相互依靠,共同抵御风浪,携手驶向成功的彼岸。然而,当航向开始产生分歧,或是波涛汹涌的风浪改变了我们的初衷,我们或许应当冷静地选择和平分手,而非在风雨中硬撑。最近,一位网友的遭遇引起了广大职场人的关注和热议。这位网友
开发者全社区
0
金融研究 | 使用Python测量关键审计事项的「信息含量」
Tips: 公众号推送后内容只能更改一次,且只能改20字符。如果内容出问题,或者想更新内容, 只能重复推送。为了更好的阅读体验,建议阅读本文博客版, 链接地址https://textdata.cn/blog/2023-01-13-information-content-of-critical-aud
大邓和他的Python
0
我看阿里的年终奖总算发了!
到4月底了,这两天看朋友圈,发现阿里的年终奖终于发了,问了问老同学,也从网上检索了不少信息,基本搞清楚了阿里今年的年终奖情况。近来来阿里一些集团对绩效等级做了较大的调整,以前的旧绩效系统中,绩效分为3.25、3.5、3.75、4和5五个等级,其中4和5是较高绩效等级,较少见。而且之前3.5绩效内部划
公子龙
0
CVPR 2024|大视觉模型的开山之作!无需任何语言数据即可打造大视觉模型
↑ 点击蓝字 关注极市平台作者丨科技猛兽编辑丨极市平台极市导读 本文提出一种序列建模 (sequential modeling) 的方法,不使用任何语言数据,训练大视觉模型。>>加入极市CV技术交流群,走在计算机视觉的最前沿本文目录1 序列建模打造大视觉模型(来自 U
极市平台
1
金融研究(更新) | 使用Python构建关键审计事项的「信息含量」
Tips: 公众号推送后内容只能更改一次,且只能改20字符。如果内容出问题,或者想更新内容, 只能重复推送。为了更好的阅读体验,建议阅读本文博客版, 链接地址https://textdata.cn/blog/2023-01-13-information-content-of-critical-aud
大邓和他的Python
0
字节的跳动职级与薪资(2024年)
上一篇:阿里公布年终奖,P7, 3.5+,22W年终奖,还有35W长期现金激励,真香字节跳动自2012年3月成立以来,已经迅速成长为一个全球性的科技公司。其产品和服务已经遍布全球150多个国家与地区,并且支持超过75种不同的语言。在字节跳动的官方网站上,列出了一系列引人注目的产品和服务,包括但不限于
开发者全社区
0
盘点Lombok的几个骚操作,你绝对没用过!
👉 欢迎加入小哈的星球 ,你将获得: 专属的项目实战 / Java 学习路线 / 一对一提问 / 学习打卡 / 赠书福利全栈前后端分离博客项目 2.0 版本完结啦, 演示链接:http://116.62.199.48/ ,新项目正在酝酿中
小哈学Java
0