- 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.
推薦閱讀
- Getting Started with ResearchKit
- Raspberry Pi for Secret Agents(Third Edition)
- C#程序設計(慕課版)
- Android 7編程入門經典:使用Android Studio 2(第4版)
- Learning ArcGIS Pro
- Windows Phone 7.5:Building Location-aware Applications
- 第一行代碼 C語言(視頻講解版)
- 基于SpringBoot實現:Java分布式中間件開發入門與實戰
- 智能手機APP UI設計與應用任務教程
- Arduino計算機視覺編程
- Arduino可穿戴設備開發
- Flask Web開發:基于Python的Web應用開發實戰(第2版)
- 進入IT企業必讀的324個Java面試題
- Python大規模機器學習
- 青少年學Python(第2冊)