feat: update utils

This commit is contained in:
antfu 2020-07-24 16:04:53 +08:00
parent 169f8429c8
commit aa3d3ae84f
10 changed files with 67 additions and 57 deletions

View File

@ -3,9 +3,18 @@
// type MyReturnType<T extends (...args: any) => any> = T
/* _____________ Test Cases _____________ */
import { expectType, _ } from '@type-challenges/utils'
import { Equal, Expect } from '@type-challenges/utils'
expectType<string>(_ as MyReturnType<() => string>)
expectType<123>(_ as MyReturnType<() => 123>)
expectType<{a: [12, 'foo']; bar: 'hello'}>(_ as MyReturnType<() => {a: [12, 'foo']; bar: 'hello'}>)
expectType<Promise<boolean>>(_ as MyReturnType<() => Promise<boolean>>)
type ComplexObject = {
a: [12, 'foo']
bar: 'hello'
prev(): number
}
type cases = [
Expect<Equal<string, MyReturnType<() => string>>>,
Expect<Equal<123, MyReturnType<() => 123>>>,
Expect<Equal<ComplexObject, MyReturnType<() => ComplexObject>>>,
Expect<Equal<Promise<boolean>, MyReturnType<() => Promise<boolean>>>>,
Expect<Equal<() => 'foo', MyReturnType<() => () => 'foo'>>>
]

View File

@ -3,7 +3,7 @@
</p>
<pre align='center'>
Collection of TypeScript typing challenges
Collection of TypeScript type challenges
</pre>
<br>
@ -11,6 +11,10 @@ Collection of TypeScript typing challenges
## Intro
<p align='center'>
<del><em>by the power of TypeScript's well-known <a href="https://github.com/microsoft/TypeScript/issues/14833">Turing Completed</a> type system</em></del>
</p>
> TODO
## Challenges
@ -34,6 +38,7 @@ Collection of TypeScript typing challenges
Inspired by / Ported from
- [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)
Contributors

View File

@ -8,7 +8,6 @@
"license": "MIT",
"scripts": {
"preinstall": "npx only-allow pnpm",
"utils:build": "pnpm -C utils build",
"utils:release": "pnpm -C utils release"
},
"devDependencies": {
@ -18,6 +17,7 @@
"@types/lz-string": "^1.3.34",
"@types/node": "^14.0.24",
"@typescript-eslint/eslint-plugin": "^3.7.0",
"@typescript-eslint/parser": "^3.7.0",
"eslint": "^7.5.0",
"fast-glob": "^3.2.4",
"js-yaml": "^3.14.0",

13
pnpm-lock.yaml generated
View File

@ -8,7 +8,8 @@ importers:
'@types/js-yaml': 3.12.5
'@types/lz-string': 1.3.34
'@types/node': 14.0.24
'@typescript-eslint/eslint-plugin': 3.7.0_eslint@7.5.0+typescript@3.9.7
'@typescript-eslint/eslint-plugin': 3.7.0_a7d792035c43f2b80bcf395c5516fe5c
'@typescript-eslint/parser': 3.7.0_eslint@7.5.0+typescript@3.9.7
eslint: 7.5.0
fast-glob: 3.2.4
js-yaml: 3.14.0
@ -25,6 +26,7 @@ importers:
'@types/lz-string': ^1.3.34
'@types/node': ^14.0.24
'@typescript-eslint/eslint-plugin': ^3.7.0
'@typescript-eslint/parser': ^3.7.0
eslint: ^7.5.0
fast-glob: ^3.2.4
js-yaml: ^3.14.0
@ -37,14 +39,8 @@ importers:
utility-types: ^3.10.0
utils:
devDependencies:
rollup: 2.22.2
rollup-plugin-dts: 1.4.9_rollup@2.22.2+typescript@3.9.7
rollup-plugin-typescript2: 0.27.1_rollup@2.22.2+typescript@3.9.7
typescript: 3.9.7
specifiers:
rollup: ^2.22.2
rollup-plugin-dts: ^1.4.9
rollup-plugin-typescript2: ^0.27.1
typescript: ^3.9.7
lockfileVersion: 5.1
packages:
@ -254,9 +250,10 @@ packages:
optional: true
resolution:
integrity: sha512-06lfjo76naNeOMDl+mWG9Fh/a0UHKLGhin+mGaIw72FUMbMGBkdi/FEJmgEDzh4eE73KIYzHWvOCYJ0ak7nrJQ==
/@typescript-eslint/eslint-plugin/3.7.0_eslint@7.5.0+typescript@3.9.7:
/@typescript-eslint/eslint-plugin/3.7.0_a7d792035c43f2b80bcf395c5516fe5c:
dependencies:
'@typescript-eslint/experimental-utils': 3.7.0_eslint@7.5.0+typescript@3.9.7
'@typescript-eslint/parser': 3.7.0_eslint@7.5.0+typescript@3.9.7
debug: 4.1.1
eslint: 7.5.0
functional-red-black-tree: 1.0.1

View File

@ -7,7 +7,7 @@
"esModuleInterop": true,
"moduleResolution": "node",
"skipLibCheck": true,
"noUnusedLocals": true,
"noUnusedLocals": false,
"noUnusedParameters": true,
"noImplicitReturns": true,
},

18
utils/index.d.test.ts Normal file
View File

@ -0,0 +1,18 @@
import { Equal, Expect, NotEqual } from './index'
type cases = [
Expect<Equal<true, true>>,
// @ts-expect-error
Expect<Equal<false, true>>,
Expect<NotEqual<false, true>>,
// @ts-expect-error
Expect<NotEqual<true, true>>,
Expect<Equal<'123', '123'>>,
// @ts-expect-error
Expect<Equal<'123', string>>,
]

19
utils/index.d.ts vendored Normal file
View File

@ -0,0 +1,19 @@
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 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 ExpectExtends<VALUE, EXPECTED> = EXPECTED extends VALUE ? true : false
export type ExpectValidArgs<FUNC extends (...args: any[]) => any, ARGS extends any[]> = ARGS extends Parameters<FUNC>
? true
: false
export type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never

View File

@ -2,22 +2,14 @@
"name": "@type-challenges/utils",
"version": "0.0.2-beta.1",
"license": "MIT",
"main": "dist/index.js",
"module": "dist/index.esm.js",
"browser": "dist/index.esm.js",
"typing": "dist/index.d.ts",
"types": "dist/index.d.ts",
"types": "index.d.ts",
"files": [
"dist"
"index.d.ts"
],
"scripts": {
"build": "rollup -c",
"release": "pnpx bumpp --commit --tag && pnpm build && pnpm publish"
"release": "pnpx bumpp --commit --tag && pnpm publish"
},
"devDependencies": {
"rollup": "^2.22.2",
"rollup-plugin-dts": "^1.4.9",
"rollup-plugin-typescript2": "^0.27.1",
"typescript": "^3.9.7"
}
}

View File

@ -1,27 +0,0 @@
import typescript from 'rollup-plugin-typescript2'
import dts from 'rollup-plugin-dts'
export default [
{
input: 'src/index.ts',
output: [
{
file: 'dist/index.js',
format: 'cjs',
},
{
file: 'dist/index.esm.js',
format: 'esm',
},
],
plugins: [typescript()],
},
{
input: 'src/index.ts',
output: {
file: 'dist/index.d.ts',
format: 'es',
},
plugins: [dts()],
},
]

View File

@ -1,3 +0,0 @@
export declare function expectType<T>(value: T): void
export declare function expectError<T>(value: T): void
export const _: any = undefined