15道优秀的TypeScript练习题(上)
connygpt 2024-11-28 08:09 14 浏览
作者:Peter谭金杰
转发链接:https://segmentfault.com/a/1190000023295719
TypeScript是目前不得不学的内容
- Ts的东西其实非常非常的多,上到tsconfig的配置,下到写法,内容。
- Ts正在疯狂的迭代,进入4.0版本即将,里面的内容非常非常的多,可以说,入门很简单,但是要写精通,真的还是要花很多功夫。
- 本文一共分上、下集,前端、后端、源码、架构、算法、面试都有,更有理财,心理学、开源项目等日常分享。
正式开始
- 第一题,基本interface使用考察,定义一个item接口,符合使用
interface item {
name: string;
age: number;
occupation: string;
}
const users: item[] = [
{
name: 'Max Mustermann',
age: 25,
occupation: 'Chimney sweep',
},
{
name: 'Kate Müller',
age: 23,
occupation: 'Astronaut',
},
];
function logPerson(user: item) {
console.log(` - ${chalk.green(user.name)}, ${user.age}`);
}
console.log(chalk.yellow('Users:'));
users.forEach(logPerson);
- 第二题,考察联合类型,让logPerson函数不报错
interface User {
name: string;
age: number;
occupation: string;
}
interface Admin {
name: string;
age: number;
role: string;
}
type Person = User | Admin;
const persons: Person[] /* <- Person[] */ = [
{
name: 'Max Mustermann',
age: 25,
occupation: 'Chimney sweep',
},
{
name: 'Jane Doe',
age: 32,
role: 'Administrator',
},
{
name: 'Kate Müller',
age: 23,
occupation: 'Astronaut',
},
{
name: 'Bruce Willis',
age: 64,
role: 'World saver',
},
];
function logPerson(user: Person) {
console.log(` - ${chalk.green(user.name)}, ${user.age}`);
}
persons.forEach(logPerson);
- 第三题,类型推断、联合类型、类型断言(此题我觉得不是最优解法,欢迎大家指出),让logPerson不报错
interface User {
name: string;
age: number;
occupation: string;
}
interface Admin {
name: string;
age: number;
role: string;
}
type Person = User | Admin;
const persons: Person[] = [
{
name: 'Max Mustermann',
age: 25,
occupation: 'Chimney sweep',
},
{
name: 'Jane Doe',
age: 32,
role: 'Administrator',
},
{
name: 'Kate Müller',
age: 23,
occupation: 'Astronaut',
},
{
name: 'Bruce Willis',
age: 64,
role: 'World saver',
},
];
function logPerson(person: Person) {
let additionalInformation: string;
if ((person as Admin).role) {
additionalInformation = (person as Admin).role;
} else {
additionalInformation = (person as User).occupation;
}
console.log(
` - ${chalk.green(person.name)}, ${person.age}, ${additionalInformation}`
);
}
persons.forEach(logPerson);
- 第四题,我这里同样使用了类型断言和类型推断、联合类型解题(感觉也不是最优),让logPerson不报错
interface User {
type: 'user';
name: string;
age: number;
occupation: string;
}
interface Admin {
type: 'admin';
name: string;
age: number;
role: string;
}
type Person = User | Admin;
const persons: Person[] = [
{
type: 'user',
name: 'Max Mustermann',
age: 25,
occupation: 'Chimney sweep',
},
{ type: 'admin', name: 'Jane Doe', age: 32, role: 'Administrator' },
{ type: 'user', name: 'Kate Müller', age: 23, occupation: 'Astronaut' },
{ type: 'admin', name: 'Bruce Willis', age: 64, role: 'World saver' },
];
function isAdmin(person: Person) {
return person.type === 'admin';
}
function isUser(person: Person) {
return person.type === 'user';
}
function logPerson(person: Person) {
let additionalInformation: string = '';
if (isAdmin(person)) {
additionalInformation = (person as Admin).role;
}
if (isUser(person)) {
additionalInformation = (person as User).occupation;
}
console.log(
` - ${chalk.green(person.name)}, ${person.age}, ${additionalInformation}`
);
}
console.log(chalk.yellow('Admins:'));
persons.filter(isAdmin).forEach(logPerson);
console.log();
console.log(chalk.yellow('Users:'));
persons.filter(isUser).forEach(logPerson);
- 第五题,我使用了索引签名解题,保证filterUsers函数不报错
interface User {
type: 'user';
name: string;
age: number;
occupation: string;
}
interface Admin {
type: 'admin';
name: string;
age: number;
role: string;
}
type Person = User | Admin;
const persons: Person[] = [
{
type: 'user',
name: 'Max Mustermann',
age: 25,
occupation: 'Chimney sweep',
},
{
type: 'admin',
name: 'Jane Doe',
age: 32,
role: 'Administrator',
},
{
type: 'user',
name: 'Kate Müller',
age: 23,
occupation: 'Astronaut',
},
{
type: 'admin',
name: 'Bruce Willis',
age: 64,
role: 'World saver',
},
{
type: 'user',
name: 'Wilson',
age: 23,
occupation: 'Ball',
},
{
type: 'admin',
name: 'Agent Smith',
age: 23,
role: 'Administrator',
},
];
const isAdmin = (person: Person): person is Admin => person.type === 'admin';
const isUser = (person: Person): person is User => person.type === 'user';
function logPerson(person: Person) {
let additionalInformation: string = '';
if (isAdmin(person)) {
additionalInformation = person.role;
}
if (isUser(person)) {
additionalInformation = person.occupation;
}
console.log(
` - ${chalk.green(person.name)}, ${person.age}, ${additionalInformation}`
);
}
function filterUsers(
persons: Person[],
criteria: { age: number; [index: string]: number }
): User[] {
return persons.filter(isUser).filter((user) => {
let criteriaKeys = Object.keys(criteria) as (keyof User)[];
return criteriaKeys.every((fieldName) => {
return user[fieldName] === criteria[fieldName];
});
});
}
console.log(chalk.yellow('Users of age 23:'));
filterUsers(persons, {
age: 23,
}).forEach(logPerson);
- 第六题,考察overloads,我对filterPersons单独进行了处理,解题,保证logPerson函数可以返回不同的类型数据
interface User {
type: 'user';
name: string;
age: number;
occupation: string;
}
interface Admin {
type: 'admin';
name: string;
age: number;
role: string;
}
type Person = User | Admin;
const persons: Person[] = [
{
type: 'user',
name: 'Max Mustermann',
age: 25,
occupation: 'Chimney sweep',
},
{ type: 'admin', name: 'Jane Doe', age: 32, role: 'Administrator' },
{ type: 'user', name: 'Kate Müller', age: 23, occupation: 'Astronaut' },
{ type: 'admin', name: 'Bruce Willis', age: 64, role: 'World saver' },
{ type: 'user', name: 'Wilson', age: 23, occupation: 'Ball' },
{ type: 'admin', name: 'Agent Smith', age: 23, role: 'Anti-virus engineer' },
];
function logPerson(person: Person) {
console.log(
` - ${chalk.green(person.name)}, ${person.age}, ${
person.type === 'admin' ? person.role : person.occupation
}`
);
}
function filterPersons(
persons: Person[],
personType: 'user',
criteria: { [fieldName: string]: number }
): User[];
function filterPersons(
persons: Person[],
personType: 'admin',
criteria: { [fieldName: string]: number }
): Admin[];
function filterPersons(
persons: Person[],
personType: string,
criteria: { [fieldName: string]: number }
) {
return persons
.filter((person) => person.type === personType)
.filter((person) => {
let criteriaKeys = Object.keys(criteria) as (keyof Person)[];
return criteriaKeys.every((fieldName) => {
return person[fieldName] === criteria[fieldName];
});
});
}
let usersOfAge23: User[] = filterPersons(persons, 'user', { age: 23 });
let adminsOfAge23: Admin[] = filterPersons(persons, 'admin', { age: 23 });
console.log(chalk.yellow('Users of age 23:'));
usersOfAge23.forEach(logPerson);
console.log();
console.log(chalk.yellow('Admins of age 23:'));
adminsOfAge23.forEach(logPerson);
- 第七题,考察泛型使用,根据传入参数不同,动态返回不同类型的数据,保证swap函数运行正常
interface User {
type: 'user';
name: string;
age: number;
occupation: string;
}
interface Admin {
type: 'admin';
name: string;
age: number;
role: string;
}
function logUser(user: User) {
const pos = users.indexOf(user) + 1;
console.log(
` - #${pos} User: ${chalk.green(user.name)}, ${user.age}, ${
user.occupation
}`
);
}
function logAdmin(admin: Admin) {
const pos = admins.indexOf(admin) + 1;
console.log(
` - #${pos} Admin: ${chalk.green(admin.name)}, ${admin.age}, ${admin.role}`
);
}
const admins: Admin[] = [
{
type: 'admin',
name: 'Will Bruces',
age: 30,
role: 'Overseer',
},
{
type: 'admin',
name: 'Steve',
age: 40,
role: 'Steve',
},
];
const users: User[] = [
{
type: 'user',
name: 'Moses',
age: 70,
occupation: 'Desert guide',
},
{
type: 'user',
name: 'Superman',
age: 28,
occupation: 'Ordinary person',
},
];
function swap<T, L>(v1: T, v2: L): [L, T] {
return [v2, v1];
}
function test1() {
console.log(chalk.yellow('test1:'));
const [secondUser, firstAdmin] = swap(admins[0], users[1]);
logUser(secondUser);
logAdmin(firstAdmin);
}
function test2() {
console.log(chalk.yellow('test2:'));
const [secondAdmin, firstUser] = swap(users[0], admins[1]);
logAdmin(secondAdmin);
logUser(firstUser);
}
function test3() {
console.log(chalk.yellow('test3:'));
const [secondUser, firstUser] = swap(users[0], users[1]);
logUser(secondUser);
logUser(firstUser);
}
function test4() {
console.log(chalk.yellow('test4:'));
const [firstAdmin, secondAdmin] = swap(admins[1], admins[0]);
logAdmin(firstAdmin);
logAdmin(secondAdmin);
}
function test5() {
console.log(chalk.yellow('test5:'));
const [stringValue, numericValue] = swap(123, 'Hello World');
console.log(` - String: ${stringValue}`);
console.log(` - Numeric: ${numericValue}`);
}
[test1, test2, test3, test4, test5].forEach((test) => test());
- 第八题,考察Omit和多类型&的使用,使用Omit提取type字段,最小代价完成了这道题
interface User {
type: 'user';
name: string;
age: number;
occupation: string;
}
interface Admin {
type: 'admin';
name: string;
age: number;
role: string;
}
type Person = User | Admin | PowerUser;
const persons: Person[] = [
{ type: 'user', name: 'Max Mustermann', age: 25, occupation: 'Chimney sweep' },
{ type: 'admin', name: 'Jane Doe', age: 32, role: 'Administrator' },
{ type: 'user', name: 'Kate Müller', age: 23, occupation: 'Astronaut' },
{ type: 'admin', name: 'Bruce Willis', age: 64, role: 'World saver' },
{
type: 'powerUser',
name: 'Nikki Stone',
age: 45,
role: 'Moderator',
occupation: 'Cat groomer'
}
];
type PowerUser = Omit<User, 'type'> & Omit<Admin, 'type'> & {type: 'powerUser'};
function isAdmin(person: Person): person is Admin {
return person.type === 'admin';
}
function isUser(person: Person): person is User {
return person.type === 'user';
}
function isPowerUser(person: Person): person is PowerUser {
return person.type === 'powerUser';
}
function logPerson(person: Person) {
let additionalInformation: string = '';
if (isAdmin(person)) {
additionalInformation = person.role;
}
if (isUser(person)) {
additionalInformation = person.occupation;
}
if (isPowerUser(person)) {
additionalInformation = `${person.role}, ${person.occupation}`;
}
console.log(`${chalk.green(person.name)}, ${person.age}, ${additionalInformation}`);
}
console.log(chalk.yellow('Admins:'));
persons.filter(isAdmin).forEach(logPerson);
console.log();
console.log(chalk.yellow('Users:'));
persons.filter(isUser).forEach(logPerson);
console.log();
console.log(chalk.yellow('Power users:'));
persons.filter(isPowerUser).forEach(logPerson);
写给读者
- 前面八道题并不一定是最优解法,Ts里面东西确实多,如果你有好的解法可以头条私信我或者下方评论
- 后面会补充剩下的题目,由易到难
- 不定期补充前端架构师面试题、各种技术的系列学习题目
推荐TypeScript知识点文章
「干货」将数十万行CoffeeScript代码迁移到TypeScript
TypeScript 中的顶级类型:any 和 unknown
1500行TypeScript代码在React中实现组件keep-alive
「TypeScript」详解一个了不起的 tsconfig.json 指南
你需要的 React + TypeScript 50 条规范和经验
Vue3.0 尝鲜 Hook TypeScript 取代 Vuex【项目实践】
「新消息」基于JavaScript/TypeScript 编程环境Deno1.0 即将发布
「干货」一张页面引起的项目架构思考(rax+Typescript+hooks)
深入浅出Vue3 跟着尤雨溪学 TypeScript 之 Ref 【实践】
作者:Peter谭金杰
转发链接:https://segmentfault.com/a/1190000023295719
相关推荐
- 自学Python,写一个挨打的游戏代码来初识While循环
-
自学Python的第11天。旋转~跳跃~,我~闭着眼!学完循环,沐浴着while的光芒,闲来无事和同事一起扯皮,我说:“编程语言好神奇,一个小小的循环,竟然在生活中也可以找到原理和例子”,同事也...
- 常用的 Python 工具与资源,你知道几个?
-
最近几年你会发现,越来越多的人开始学习Python,工欲善其事必先利其器,今天纬软小编就跟大家分享一些常用的Python工具与资源,记得收藏哦!不然下次就找不到我了。1、PycharmPychar...
- 一张思维导图概括Python的基本语法, 一周的学习成果都在里面了
-
一周总结不知不觉已经自学Python一周的时间了,这一周,从认识Python到安装Python,再到基本语法和基本数据类型,对于小白的我来说无比艰辛的,充满坎坷。最主要的是每天学习时间有限。只...
- 三日速成python?打工人,小心钱包,别当韭菜
-
随着人工智能的热度越来越高,许多非计算机专业的同学们也都纷纷投入到学习编程的道路上来。而Python,作为一种相对比较容易上手的语言,也越来越受欢迎。网络上各类网课层出不穷,各式广告令人眼花缭乱。某些...
- Python自动化软件测试怎么学?路线和方法都在这里了
-
Python自动化测试是指使用Python编程语言和相关工具,对软件系统进行自动化测试的过程。学习Python自动化测试需要掌握以下技术:Python编程语言:学习Python自动化测试需要先掌握Py...
- Python从放弃到入门:公众号历史文章爬取为例谈快速学习技能
-
这篇文章不谈江流所专研的营销与运营,而聊一聊技能学习之路,聊一聊Python这门最简单的编程语言该如何学习,我完成的第一个Python项目,将任意公众号的所有历史文章导出成PDF电子书。或许我这个Py...
- 【黑客必会】python学习计划
-
阅读Python文档从Python官方网站上下载并阅读Python最新版本的文档(中文版),这是学习Python的最好方式。对于每个新概念和想法,请尝试运行一些代码片段,并检查生成的输出。这将帮助您更...
- 公布了!2025CDA考试安排
-
CDA数据分析师报考流程数据分析师是指在不同行业中专门从事行业数据搜集、整理、分析依据数据作出行业研究评估的专业人员CDA证书分为1-3级,中英文双证就业面广,含金量高!!?报考条件:满18...
- 一文搞懂全排列、组合、子集问题(经典回溯递归)
-
原创公众号:【bigsai】头条号:程序员bigsai前言Hello,大家好,我是bigsai,longtimenosee!在刷题和面试过程中,我们经常遇到一些排列组合类的问题,而全排列、组合...
- 「西法带你学算法」一次搞定前缀和
-
我花了几天时间,从力扣中精选了五道相同思想的题目,来帮助大家解套,如果觉得文章对你有用,记得点赞分享,让我看到你的认可,有动力继续做下去。467.环绕字符串中唯一的子字符串[1](中等)795.区...
- 平均数的5种方法,你用过几种方法?
-
平均数,看似很简单的东西,其实里面包含着很多学问。今天,分享5种经常会用到的平均数方法。1.算术平均法用到最多的莫过于算术平均法,考试平均分、平均工资等等,都是用到这个。=AVERAGE(B2:B11...
- 【干货收藏】如何最简单、通俗地理解决策树分类算法?
-
决策树(Decisiontree)是基于已知各种情况(特征取值)的基础上,通过构建树型决策结构来进行分析的一种方式,是常用的有监督的分类算法。决策树算法是机器学习中的一种经典算法,它通过一系列的规则...
- 面试必备:回溯算法详解
-
我们刷leetcode的时候,经常会遇到回溯算法类型题目。回溯算法是五大基本算法之一,一般大厂也喜欢问。今天跟大家一起来学习回溯算法的套路,文章如果有不正确的地方,欢迎大家指出哈,感谢感谢~什么是回溯...
- 「机器学习」决策树——ID3、C4.5、CART(非常详细)
-
决策树是一个非常常见并且优秀的机器学习算法,它易于理解、可解释性强,其可作为分类算法,也可用于回归模型。本文将分三篇介绍决策树,第一篇介绍基本树(包括ID3、C4.5、CART),第二篇介绍Ran...
- 大话AI算法: 决策树
-
所谓的决策树算法,通俗的说就是建立一个树形的结构,通过这个结构去一层一层的筛选判断问题是否好坏的算法。比如判断一个西瓜是否好瓜,有20条西瓜的样本提供给你,让你根据这20条(通过机器学习)建立起...
- 一周热门
- 最近发表
- 标签列表
-
- kubectlsetimage (56)
- mysqlinsertoverwrite (53)
- addcolumn (54)
- helmpackage (54)
- varchar最长多少 (61)
- 类型断言 (53)
- protoc安装 (56)
- jdk20安装教程 (60)
- rpm2cpio (52)
- 控制台打印 (63)
- 401unauthorized (51)
- vuexstore (68)
- druiddatasource (60)
- 企业微信开发文档 (51)
- rendertexture (51)
- speedphp (52)
- gitcommit-am (68)
- bashecho (64)
- str_to_date函数 (58)
- yum下载包及依赖到本地 (72)
- jstree中文api文档 (59)
- mvnw文件 (58)
- rancher安装 (63)
- nginx开机自启 (53)
- .netcore教程 (53)