函数是一种承诺。它接收某物,做某件事,然后返回某物。本课学习如何阅读 TypeScript 函数签名——这就像阅读一份英文合同。
A function is a promise. It takes something, does something, and returns something. Learn how TypeScript makes those promises explicit — and how reading function signatures is just reading English contracts.
代码精读
// A function signature tells you a complete story in one line:
// WHO takes WHAT and returns WHAT
function greetUser(name: string): string {
return `Hello, ${name}! Welcome to TypeSpeak.`;
}
// 📖 Reading this signature in English:
// 'greetUser receives a name (string) and returns a greeting (string)'
// Functions with multiple parameters read like a recipe:
function calculateReadingTime(
wordCount: number,
wordsPerMinute: number = 200 // default value = assumption
): number {
return Math.ceil(wordCount / wordsPerMinute);
}
// Arrow functions — same idea, more concise:
const capitalize = (word: string): string =>
word.charAt(0).toUpperCase() + word.slice(1);
💡 建议:把每一行代码大声用英文念出来,注释就是你的翻译。
单词讲解
parameter
/pəˈræmɪtər/
noun
参数(函数定义中的输入项)
argument
/ˈɑːrɡjəmənt/
noun
实参(调用函数时传入的具体值)
return type
/rɪˈtɜːrn taɪp/
noun phrase
返回类型(函数输出的数据类型)
concise
/kənˈsaɪs/
adjective
简洁的,言简意赅的
理解检验
先在脑子里想一想——再点开看参考答案。
Q1: What does the `: string` after `greetUser(name: string)` mean?
Q2: What is the difference between a parameter and an argument?
核心要点▼ 展开