TypeScript

TypeScript

简介

TypeScript 是由微软进行开发和维护的一种开源的编程语言。 TypeScript 是 JavaScript 的严格语法超集,提供了可选的静态类型检查。

基本写法

读取特定 key

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
const myObject = {
a: 'apple',
b: 'banana',
c: 'cherry'
};

function getValueFromKey(key: string): string | null {
if (key in myObject) {
const value = myObject[<keyof typeof myObject>key];
console.log(value);
return value;
} else {
console.log("Key not found");
return null;
}
}

console.log(getValueFromKey('a')); // Output: 'apple'
console.log(getValueFromKey('b')); // Output: 'banana'
console.log(getValueFromKey('c')); // Output: 'cherry'
console.log(getValueFromKey('d')); // Output: "Key not found"

参考资料

维基百科

官方项目

官方文档


TypeScript
https://wangqian0306.github.io/2023/typescript/
作者
WangQian
发布于
2023年11月9日
许可协议