- Learn React with TypeScript 3
- Carl Rippon
- 158字
- 2021-06-10 19:16:42
Open-ended tuples
Before TypeScript 3, tuples had to have a fixed amount of elements. TypeScript 3 gives us a little more flexibility with rest elements. rest elements are similar to rest parameters, described in the last section, but they work with tuple element types. A rest element allows us to define an open-ended tuple.
Time to go through an example:
- In the TypeScript playground, let's create a tuple with the first element being a string and subsequent elements being numbers:
type Scores = [string, ...number[]];
- We should be able to use this structure to store someone's name with an infinite amount of scores. Let's give this a go for Billy and three scores:
const billyScores: Scores = ["Billy", 60, 70, 75];
- Let's move on to try Sally and four scores:
const sallyScores: Scores = ["Sally", 60, 70, 75, 70];
Both these variables compile fine, as we would expect, because we have defined the numbers as open-ended.
推薦閱讀
- 潮流:UI設(shè)計(jì)必修課
- Building a RESTful Web Service with Spring
- Instant Typeahead.js
- SEO實(shí)戰(zhàn)密碼
- Building a Quadcopter with Arduino
- INSTANT Django 1.5 Application Development Starter
- 領(lǐng)域驅(qū)動(dòng)設(shè)計(jì):軟件核心復(fù)雜性應(yīng)對(duì)之道(修訂版)
- C#實(shí)踐教程(第2版)
- Node.js開發(fā)指南
- 寫給程序員的Python教程
- Mastering Concurrency Programming with Java 9(Second Edition)
- 零基礎(chǔ)學(xué)C語(yǔ)言(第4版)
- 計(jì)算語(yǔ)言學(xué)導(dǎo)論
- Manage Your SAP Projects with SAP Activate
- Learning ROS for Robotics Programming