Type-Challenges
这里记录一些基于type-challenges的一些挑战题目来学习的
typescript
type
笔记
背景
1 | // 以下这三个类型均来源于 '@type-challenges/utils' 这些类型将会在接下来用于判断挑战的结果。这里简单介绍以下这三个类型 |
Hello World
[挑战地址](https://www.typescriptlang.org/play?# code/PQKgUABBCMDMEFoIAkCmAbdB7CB1LATugCaSIIWVkBGAnhAIIB2ALgBZZP0BiArhAAoAAgENWAM14BKCAGJAtw6BquIhkys9REAZGYDu3VVDSYsAGjyESAQj0RAm-GAqOUAB3oFVlQCFugBeNAd6mAYPUDY5oErowAbygPfKgKdygNf6gPgJgFBygNJGEAAqtAAOqBAAwmwimKhMAOaoAM6AQAyAFOrWgDOJgDD-gIhGgDdygP7ygBSugGFygABygNlygHqegOKazoABRoDnur7xSagAygDGBACWiSwQgFgJgOPxgFeBis6A3j6A0eqA3z6A+36ApuaAedqADc7LgBVKgCRKmoUqUGSltoCIxoAYRoC0chX+gADpgIGRgHfygJymgGg5QCdps5AMdygEAPOqAdP1ANK2gFXopaACH+6oA3RUAhTaASHMtoBMVMA99GASH-ClYAAZklj5MjAYAQQD45t5APRmLVsEHyLGmuQgWzILBGKAw2HwRGIEAAvBAxLQyGSSaTyZSoNSIF9vjDAEGagOsgBkIwCwcoAseUA2UaAL8VAED6gA49Hl8lgFObigCiAA9kuMWAAeO0AR14mVdBkFZmIJjZHJyAD5Q9KyVZAJ0OgG-FSKgiAkwClxoB15ReJIggAB9QDTmoA0fwhgEB-wCjpoAUBMAkIahQtWUMQYoQADiU3YvGoEEigFPzQDQ7oAsf7YLBYiXyAC5qRTxmwAHQAK3yk8IOWAcGAAC82AhUgA5MAgYBgfegCAAfRPp7Pp+V4Ihx-Pt6PEF3+95yX5hiFJDFEq4ECVqCdqBdVARRYHBqBSERWXZKZcn3MBDzvW8IARBpAGj5G8EIvR8pgAW0SQg5gAbwgT1vXQExHWdFgTE3LAWGYegAF8IHEAgsGwiAAHIhGfVAEAnTJ0GyPJ8mAXgWCmdB8g4p8+XGER8gKT8AG0yAogC3RouiuF9AUsHfYhwyMVT-xdd0vR9P09IDIMoNycMwAAXVguCQHQjD70BFpAFNrNyMIfPdQDIWtADAlQBquX8ZxAGPIwAVbz7Ach1HYBxynWd5wIRdlzEfIAHdUAIVd1y3IKIG7GK4sHEcx3yCcZznBcl1gYB8iwdAxKmTgFQgWtABezQAsTR0cqEqqmrUvqtcN23R8wCAA)
1 | // 期望是一个 string 类型 |
答案
1 | // 期望是一个 string 类型 |
解释
如题就是一个Hello World
。题目要求HelloWorld的类型是string。
直接在定义处给string即可
type HelloWorld = string
知识点
这个hello world只需要读懂type test = Expect<Equal<HelloWorld, string>>
即可。
Pick
[挑战地址](https://www.typescriptlang.org/play# code/PQKgUABBAsELQUHnagG5wgBQJYGMDWl5yFH4BGAnhAIIB2ALgBYD21FAYgK4QAUAAgIZ0AZuwCUEAMSAA70CqyhPbUMzCSXYYANrTgZq+fOP0RAGRmA7t11QAfBECMroHYYwDLWgK8DAFUqBv-0CL0YBh-wETWgOfj0AJQhATlNAbfj3QApYwHnEwCAGMwhAA9NAAHTAf3lACldAUMVAO39AELcAA0xcAB4AFQAaCABpc1zAbZtAaPVAZ2VAe+VYqHxALATAcfjAOblcitzAWjlASATAS6NAPR1AcgNc4tzAU3NAEPNAAgTAbx86uMBo+UAgzTjc3doAZ3xtWgBTACdBPiwTiGLGABNGCABvfChaDFo1E4AuCH3aGdtABzN4Qe4nfZYIEABw+zD+AKB1FBUCgWEYAFsYd9Tvc-iRGIxvgJ8ABfOK0Mgwm53R5oM4nABuGBOAHcIABeCAAWTIBRwJQejHKAHIPl8TqKIAAfCCijHY3Ene6i8xxDHUAEQWjCv50xgM5msjnc15otES75-UUAYRJ1AgZyJmNFpTB6KxOJOeL+lzU+xO7qgFKgu1ycUsgAp1CAAcU+9HYJAggCg5QCn5oBod0AWP-0Wi0GH7H7AYAHLD0AB0ACt9uXGGdgcBoMAAF70OC2gByYBAwDAfdAEAA+sOR6ORxBAAby2UAx3KAQA8h2PF4OID2+1Sabz+dhBWVKpZuQIyH2wAOl4uIIBpW0Aq9GpdYLs-j1cYbF12gvCAAUQAjuw+Gpyh+AAeNJYG+ZIQIIzqYvKPDriccBln+3wopCwDsB8Aaimu1I3FgfCBvsXIQAA2vgQEgbQhTfr+ahUcBJygSqACM5R8gKQqPGKVpSuYvHBp+9GgVRP5-nRFEqgATKxW5FAaXGfN80pygqXrKqqvHmPxxYQLB+xwCcgmaOczpnPgbHbhxIrytxSnyoq3p4rZoraEyf4YOp7oALp9sc5yXNctzCi8+DcYigIgvgEJQrC8LUGFyKohA9lqQSRIOmAFJgL5FxXDc5EMXiTHBe8Cm-P84UohlPl0H5uUCeJ9wScVOqlfFEWekqPoqqlxInKSmX9iA94PsugDQcoAAHKAKbWw0PiuvagPgliAGBKgDVclOgDHkYAKt45nmBZFiWUIVtWtb1o2wACPsbLnC2badotECZltO35oWxalkdNZ1g2Tb7MS6GKFq92AC9mgBYmiYz17W9h1Vp9p2tu2XarmAQA)
1 | // 通过从K中选择属性T来构造类型 |
答案
1 | /* _____________ 你的代码 _____________ */ |
解释
首先由题意得MyPick
接受了两个类型Todo
即是要从该类型中提取属性,另一个类型即Todo
中所具有的属性。所以MyPick
本身接受的类型不难构建type MyPick<T extends object, K extends keyof T>
接下来要做的操作即从遍历给出的K
中所有的属性,并从T
中返回这些属性的类型即可。[P in K]
即遍历K
中的值即输入的T
中的属性,T[P]
即从T中拿到给出的属性的类型。
知识点
keyof关键字
TypeScript 允许我们遍历某种类型的属性,并通过 keyof 操作符提取其属性的名称。**keyof
操作符是在 TypeScript 2.1 版本引入的,该操作符可以用于获取某种类型的所有键,其返回类型是联合类型。**
1 | interface Person { |
In关键字
In关键字遍历给出的类型。
1 | // 在此示例中,将从该类型中取出所有属性,并将其值更改为布尔。 |
参考 & 引用
[Feature request]type level equal operator · Issue # 27024 · microsoft/TypeScript (github.com)
TypeScript居然还能这么玩 - 知乎 (zhihu.com)
TypeScript: Documentation - TypeScript 2.1 (typescriptlang.org)