New question Tuple to Object (#16)

Co-authored-by: sinoon <sinoon1218@gmail.com>
This commit is contained in:
Anthony Fu 2020-07-29 16:27:49 +08:00 committed by GitHub
parent 06f790ca50
commit df6e22f105
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 71 additions and 38 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,14 @@
<!--info-header-start--><h1>Tuple to Object <img src="https://img.shields.io/badge/-easy-90bb12" alt="easy"/> </h1><blockquote><p>by sinoon <a href="https://github.com/sinoon" target="_blank">@sinoon</a></p></blockquote><p><a href="https://type-challenges.netlify.app/11/play" target="_blank"><img src="https://img.shields.io/badge/-Take%20the%20Challenge-3178c6?logo=typescript" alt="Take the Challenge"/></a> </p><!--info-header-end-->
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<list> // expected { tesla: 'tesla', 'model 3': 'model 3', 'model X': 'model X', 'model Y': 'model Y'}
```
<!--info-footer-start--><br><a href="../../README.md" target="_blank"><img src="https://img.shields.io/badge/-Back-grey" alt="Back"/></a> <a href="https://type-challenges.netlify.app/11/solutions" target="_blank"><img src="https://img.shields.io/badge/-Check%20out%20Solutions-de5a77?logo=awesome-lists&logoColor=white" alt="Check out Solutions"/></a> <a href="https://type-challenges.netlify.app/11/answer" target="_blank"><img src="https://img.shields.io/badge/-Share%20your%20Solutions-green" alt="Share your Solutions"/></a> <!--info-footer-end-->

View File

@ -0,0 +1,8 @@
title: Tuple to Object
author:
name: sinoon
email: sinoon1218@gamil.com
github: sinoon
tags:

View File

@ -0,0 +1 @@
title: 元组转换为对象

View File

@ -0,0 +1 @@
type TupleToObject<T extends readonly any[]> = any

View File

@ -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<Equal<TupleToObject<typeof list>, { tesla: 'tesla', 'model 3': 'model 3', 'model X': 'model X', 'model Y': 'model Y'}>>,
]

View File

@ -2,7 +2,9 @@
Hello, World!
在 Type Challenges 中,我们使用 TypeScript 自身的类型系统来实现自动判题。 在挑战中,你需要修改下方的代码使得测试通过(即使其没有类型错误)。
这个简单的提问希望让你可以快速上手 Type Challenges。在这里我们使用了一些神奇的技巧让 TypeScript 通过自身的类型系统来实现自动判题。
在这个挑战中,你需要修改下方的代码使得测试通过(使其没有类型错误)。
```ts
// 期望是一个 string 类型
@ -14,6 +16,6 @@ type HelloWorld = any
type test = Expect<Equal<HelloWorld, string>>
```
点击上方的 `接受挑战` 开始编码!旅途愉快!
点击上方的 `接受挑战` 开始编码!旅途愉快!
<!--info-footer-start--><br><a href="../../README.zh-CN.md" target="_blank"><img src="https://img.shields.io/badge/-%E8%BF%94%E5%9B%9E%E9%A6%96%E9%A1%B5-grey" alt="返回首页"/></a> <a href="https://type-challenges.netlify.app/13/solutions" target="_blank"><img src="https://img.shields.io/badge/-%E6%9F%A5%E7%9C%8B%E8%A7%A3%E7%AD%94-de5a77?logo=awesome-lists&logoColor=white" alt="查看解答"/></a> <a href="https://type-challenges.netlify.app/13/answer/zh-CN" target="_blank"><img src="https://img.shields.io/badge/-%E5%88%86%E4%BA%AB%E4%BD%A0%E7%9A%84%E8%A7%A3%E7%AD%94-green" alt="分享你的解答"/></a> <!--info-footer-end-->

View File

@ -1,20 +1,27 @@
import { Equal, Expect, NotEqual } from './index'
import { Equal, Expect, NotEqual, IsAny, ExpectFalse } from './index'
type cases = [
/* Expect */
Expect<true>,
// @ts-expect-error
Expect<false>,
/* ExpectFalse */
ExpectFalse<false>,
// @ts-expect-error
ExpectFalse<true>,
/* Equal */
Expect<Equal<true, true>>,
// @ts-expect-error
Expect<Equal<false, true>>,
Expect<NotEqual<false, true>>,
// @ts-expect-error
Expect<NotEqual<true, true>>,
ExpectFalse<Equal<false, true>>,
Expect<Equal<'123', '123'>>,
ExpectFalse<Equal<'123', string>>,
// @ts-expect-error
Expect<Equal<'123', string>>,
/* Not Equal */
Expect<NotEqual<false, true>>,
ExpectFalse<NotEqual<true, true>>,
/* IsAny */
Expect<IsAny<any>>,
ExpectFalse<IsAny<1>>,
]

15
utils/index.d.ts vendored
View File

@ -1,16 +1,13 @@
// @internal
export type NOT_EQUAL_INTERNAL<VALUE, EXPECTED> = UnionToIntersection<VALUE> extends UnionToIntersection<EXPECTED>
? UnionToIntersection<EXPECTED> extends UnionToIntersection<VALUE>
? false
: true
: true
export type Expect<T extends true> = T
export type ExpectTrue<T extends true> = T
export type ExpectFalse<T extends false> = T
export type IsTrue<T extends true> = T
export type IsFalse<T extends false> = T
export type NotEqual<VALUE, EXPECTED> = true extends NOT_EQUAL_INTERNAL<VALUE, EXPECTED> ? true : false
export type Equal<VALUE extends EXPECTED, EXPECTED> = NotEqual<VALUE, EXPECTED> extends false ? true : false
export type NotEqual<X, Y> = true extends Equal<X, Y> ? false : true
export type Equal<X, Y> =
(<T>() => T extends X ? 1 : 2) extends
(<T>() => T extends Y ? 1 : 2) ? true : false
// https://stackoverflow.com/questions/49927523/disallow-call-with-any/49928360#49928360
export type IsAny<T> = 0 extends (1 & T) ? true : false

View File

@ -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": [