From df6e22f105b8a1cc79786435c20f95e0d53db45d Mon Sep 17 00:00:00 2001
From: Anthony Fu
Date: Wed, 29 Jul 2020 16:27:49 +0800
Subject: [PATCH] New question Tuple to Object (#16)
Co-authored-by: sinoon
---
README.md | 14 +++------
README.zh-CN.md | 10 +++---
questions/11-easy-restrict-key/README.md | 14 +++++++++
questions/11-easy-restrict-key/info.yml | 8 +++++
questions/11-easy-restrict-key/info.zh-CN.yml | 1 +
questions/11-easy-restrict-key/template.ts | 1 +
questions/11-easy-restrict-key/test-cases.ts | 7 +++++
questions/13-warm-hello-world/README.zh-CN.md | 6 ++--
utils/index.d.test.ts | 31 ++++++++++++-------
utils/index.d.ts | 15 ++++-----
utils/package.json | 2 +-
11 files changed, 71 insertions(+), 38 deletions(-)
create mode 100644 questions/11-easy-restrict-key/README.md
create mode 100644 questions/11-easy-restrict-key/info.yml
create mode 100644 questions/11-easy-restrict-key/info.zh-CN.yml
create mode 100644 questions/11-easy-restrict-key/template.ts
create mode 100644 questions/11-easy-restrict-key/test-cases.ts
diff --git a/README.md b/README.md
index 0d6fd27d..1e56ec49 100644
--- a/README.md
+++ b/README.md
@@ -1,9 +1,3 @@
-
-
-----
-
@@ -19,7 +13,6 @@ Collection of TypeScript type challenges
English | 简体中文
-
## Intro
@@ -27,14 +20,16 @@ English | 简体中文
by the power of TypeScript's well-known Turing Completed type system
-> TODO
+High quality types can help with projects' maintainability and avoiding bugs. There are a bunch of awesome type utilities libraries may boosting your works on types, like [ts-toolbelt](https://github.com/millsp/ts-toolbelt), [utility-types](https://github.com/piotrwitek/utility-types), [SimplyTyped](https://github.com/andnp/SimplyTyped), etc. We took a lot of inspiration from them too!
+
+This project is aim to make you better understand how the type system works, writing your own utilities, or just having fun with the challenges. We are also trying to form a community that you can ask and answer questions you have faced in realworld - they may become part the challenges as well!
## Challenges
> Click the following badges to see detail of the challenges!
-




By Tags
+




By Tags
## Recommended Readings
@@ -69,7 +64,6 @@ Just [open an issue](https://github.com/type-challenges/type-challenges/issues/n
Inspired by
- [piotrwitek/utility-types](https://github.com/piotrwitek/utility-types)
-- [wixplosives/typescript-type-utils](https://github.com/wixplosives/typescript-type-utils)
- [psmyrdek/typescript-challenges](https://github.com/psmyrdek/typescript-challenges)
- [andnp/SimplyTyped](https://github.com/andnp/SimplyTyped)
diff --git a/README.zh-CN.md b/README.zh-CN.md
index 6678292e..465b073b 100644
--- a/README.zh-CN.md
+++ b/README.zh-CN.md
@@ -15,20 +15,22 @@ TypeScript 类型体操姿势合集
-## Intro
+## 介绍
-by the power of TypeScript's well-known Turing Completed type system
+众所周知,TypeScript 拥有一个图灵完备的类型系统
-> TODO
+高质量的类型可以提高项目的可维护性并避免一些潜在的漏洞。市面上也已经有许多优秀的类型工具库,像是 [ts-toolbelt](https://github.com/millsp/ts-toolbelt), [utility-types](https://github.com/piotrwitek/utility-types), [SimplyTyped](https://github.com/andnp/SimplyTyped) 等等。我们也从这些项目中获取了许多灵感。
+
+本项目意在于让你更好的了解 TS 的类型系统,编写你自己的类型工具,或者只是单纯的享受挑战的了乐趣!我们同时希望可以建立一个社区,在这里你可以提出你在实际环境中遇到的问题,或者帮助他人解答疑惑 - 这些问题也可能被选中成为题库的一部分!
## 题库
> 点击下方徽章查看题目内容
-




By Tags
+




By Tags
## 推荐读物
diff --git a/questions/11-easy-restrict-key/README.md b/questions/11-easy-restrict-key/README.md
new file mode 100644
index 00000000..f4994585
--- /dev/null
+++ b/questions/11-easy-restrict-key/README.md
@@ -0,0 +1,14 @@
+Tuple to Object
by sinoon @sinoon
+
+Given an array, transform to a object type and the key/value must in the given array.
+
+For example
+
+```ts
+const list = ['tesla', 'model 3', 'model X', 'model Y'] as const
+
+const object: TupleToObject // expected { tesla: 'tesla', 'model 3': 'model 3', 'model X': 'model X', 'model Y': 'model Y'}
+
+```
+
+
diff --git a/questions/11-easy-restrict-key/info.yml b/questions/11-easy-restrict-key/info.yml
new file mode 100644
index 00000000..2bcd4838
--- /dev/null
+++ b/questions/11-easy-restrict-key/info.yml
@@ -0,0 +1,8 @@
+title: Tuple to Object
+
+author:
+ name: sinoon
+ email: sinoon1218@gamil.com
+ github: sinoon
+
+tags:
diff --git a/questions/11-easy-restrict-key/info.zh-CN.yml b/questions/11-easy-restrict-key/info.zh-CN.yml
new file mode 100644
index 00000000..3618b268
--- /dev/null
+++ b/questions/11-easy-restrict-key/info.zh-CN.yml
@@ -0,0 +1 @@
+title: 元组转换为对象
diff --git a/questions/11-easy-restrict-key/template.ts b/questions/11-easy-restrict-key/template.ts
new file mode 100644
index 00000000..9bea3389
--- /dev/null
+++ b/questions/11-easy-restrict-key/template.ts
@@ -0,0 +1 @@
+type TupleToObject = any
diff --git a/questions/11-easy-restrict-key/test-cases.ts b/questions/11-easy-restrict-key/test-cases.ts
new file mode 100644
index 00000000..e0efabb8
--- /dev/null
+++ b/questions/11-easy-restrict-key/test-cases.ts
@@ -0,0 +1,7 @@
+import { Equal, Expect } from '@type-challenges/utils'
+
+const list = ['tesla', 'model 3', 'model X', 'model Y'] as const
+
+type cases = [
+ Expect, { tesla: 'tesla', 'model 3': 'model 3', 'model X': 'model X', 'model Y': 'model Y'}>>,
+]
diff --git a/questions/13-warm-hello-world/README.zh-CN.md b/questions/13-warm-hello-world/README.zh-CN.md
index ebcf477d..cc12a77f 100644
--- a/questions/13-warm-hello-world/README.zh-CN.md
+++ b/questions/13-warm-hello-world/README.zh-CN.md
@@ -2,7 +2,9 @@
Hello, World!
-在 Type Challenges 中,我们使用 TypeScript 自身的类型系统来实现自动判题。 在挑战中,你需要修改下方的代码使得测试通过(即使其没有类型错误)。
+这个简单的提问希望让你可以快速上手 Type Challenges。在这里,我们使用了一些神奇的技巧让 TypeScript 通过自身的类型系统来实现自动判题。
+
+在这个挑战中,你需要修改下方的代码使得测试通过(使其没有类型错误)。
```ts
// 期望是一个 string 类型
@@ -14,6 +16,6 @@ type HelloWorld = any
type test = Expect>
```
-点击上方的 `接受挑战` 开始编码!祝旅途愉快!
+点击上方的 `接受挑战` 开始编码!旅途愉快!
\ No newline at end of file
diff --git a/utils/index.d.test.ts b/utils/index.d.test.ts
index 2b80cee4..6add304d 100644
--- a/utils/index.d.test.ts
+++ b/utils/index.d.test.ts
@@ -1,20 +1,27 @@
-import { Equal, Expect, NotEqual } from './index'
+import { Equal, Expect, NotEqual, IsAny, ExpectFalse } from './index'
type cases = [
+ /* Expect */
+ Expect,
+ // @ts-expect-error
+ Expect,
+ /* ExpectFalse */
+ ExpectFalse,
+ // @ts-expect-error
+ ExpectFalse,
+
+ /* Equal */
Expect>,
-
- // @ts-expect-error
- Expect>,
-
- Expect>,
-
- // @ts-expect-error
- Expect>,
-
+ ExpectFalse>,
Expect>,
+ ExpectFalse>,
- // @ts-expect-error
- Expect>,
+ /* Not Equal */
+ Expect>,
+ ExpectFalse>,
+ /* IsAny */
+ Expect>,
+ ExpectFalse>,
]
diff --git a/utils/index.d.ts b/utils/index.d.ts
index 81064d52..0491fffb 100644
--- a/utils/index.d.ts
+++ b/utils/index.d.ts
@@ -1,16 +1,13 @@
-// @internal
-export type NOT_EQUAL_INTERNAL = UnionToIntersection extends UnionToIntersection
- ? UnionToIntersection extends UnionToIntersection
- ? false
- : true
- : true
-
export type Expect = T
export type ExpectTrue = T
export type ExpectFalse = T
+export type IsTrue = T
+export type IsFalse = T
-export type NotEqual = true extends NOT_EQUAL_INTERNAL ? true : false
-export type Equal = NotEqual extends false ? true : false
+export type NotEqual = true extends Equal ? false : true
+export type Equal =
+ (() => T extends X ? 1 : 2) extends
+ (() => T extends Y ? 1 : 2) ? true : false
// https://stackoverflow.com/questions/49927523/disallow-call-with-any/49928360#49928360
export type IsAny = 0 extends (1 & T) ? true : false
diff --git a/utils/package.json b/utils/package.json
index f92f92fe..f2e1f95d 100644
--- a/utils/package.json
+++ b/utils/package.json
@@ -1,6 +1,6 @@
{
"name": "@type-challenges/utils",
- "version": "0.1.0-beta.2",
+ "version": "0.1.0-beta.4",
"license": "MIT",
"types": "index.d.ts",
"files": [