Skip to content

Commit

Permalink
feat: 为其余 types 添加测试
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Nov 21, 2020
1 parent 26be1f6 commit c36971e
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 2 deletions.
33 changes: 33 additions & 0 deletions src/types/AnyAsyncFunction.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { AnyAsyncFunction } from './AnyAsyncFunction'
import { expectType } from './__expectType__'

describe('AnyAsyncFunction', () => {
test('不是异步 function', () => {
// @ts-expect-error
expectType<AnyAsyncFunction, number>()
// @ts-expect-error
expectType<AnyAsyncFunction, string>()
// @ts-expect-error
expectType<AnyAsyncFunction, boolean>()
// @ts-expect-error
expectType<AnyAsyncFunction, unknown>()
// @ts-expect-error
expectType<AnyAsyncFunction, undefined>()
// @ts-expect-error
expectType<AnyAsyncFunction, null>()
// @ts-expect-error
expectType<AnyAsyncFunction, {}>()
// @ts-expect-error
expectType<AnyAsyncFunction, []>()
// @ts-expect-error
expectType<AnyAsyncFunction, RegExp>()
// @ts-expect-error
expectType<AnyAsyncFunction, () => 0>()
})

test('是异步 function', () => {
expectType<AnyAsyncFunction, () => Promise<0>>()
// why ???
expectType<AnyAsyncFunction, never>()
})
})
31 changes: 31 additions & 0 deletions src/types/AnyFunction.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { AnyFunction } from './AnyFunction'
import { expectType } from './__expectType__'

describe('AnyFunction', () => {
test('不是 function', () => {
// @ts-expect-error
expectType<AnyFunction, number>()
// @ts-expect-error
expectType<AnyFunction, string>()
// @ts-expect-error
expectType<AnyFunction, boolean>()
// @ts-expect-error
expectType<AnyFunction, unknown>()
// @ts-expect-error
expectType<AnyFunction, undefined>()
// @ts-expect-error
expectType<AnyFunction, null>()
// @ts-expect-error
expectType<AnyFunction, {}>()
// @ts-expect-error
expectType<AnyFunction, []>()
// @ts-expect-error
expectType<AnyFunction, RegExp>()
})

test('是 function', () => {
expectType<AnyFunction, () => any>()
// why ???
expectType<AnyFunction, never>()
})
})
28 changes: 28 additions & 0 deletions src/types/AnyObject.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { AnyObject } from './AnyObject'
import { expectType } from './__expectType__'

describe('AnyObject', () => {
test('不是 object', () => {
// @ts-expect-error
expectType<AnyObject, number>()
// @ts-expect-error
expectType<AnyObject, string>()
// @ts-expect-error
expectType<AnyObject, boolean>()
// @ts-expect-error
expectType<AnyObject, unknown>()
// @ts-expect-error
expectType<AnyObject, undefined>()
// @ts-expect-error
expectType<AnyObject, null>()
})

test('是 object', () => {
expectType<AnyObject, {}>()
expectType<AnyObject, []>()
expectType<AnyObject, RegExp>()
expectType<AnyObject, () => any>()
// why ???
expectType<AnyObject, never>()
})
})
25 changes: 25 additions & 0 deletions src/types/Path.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { expectType } from './__expectType__'
import { Path, PathValue } from './Path'

describe('OneOrMore', () => {
test('表现正常', () => {
const obj = {
x: {
y: {
z: [0, '1'] as const,
哈哈: 1,
},
},
}
// eslint-disable-next-line
function get<T, L extends Path<T, L>>(object: T, path: L): PathValue<T, L> {
return 1 as any
}

const xyz1 = get(obj, ['x', 'y', 'z', '1'])
expectType<typeof xyz1, '1'>()

const xyz哈哈 = get(obj, ['x', 'y', '哈哈'])
expectType<typeof xyz哈哈, number>()
})
})
4 changes: 2 additions & 2 deletions src/types/Path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ type PathArrayValue<T, L extends PathArray<T, L>> = L extends ArrayHasIndex<
* @example
* ```typescript
* function get<T, L extends Path<T, L>>(
* object: K,
* object: T,
* path: L,
* ): PathValue<T, L> {
* // ...
Expand All @@ -136,7 +136,7 @@ export type Path<T, L> = PathArray<T, L> | keyof T
* @example
* ```typescript
* function get<T, L extends Path<T, L>>(
* object: K,
* object: T,
* path: L,
* ): PathValue<T, L> {
* // ...
Expand Down

0 comments on commit c36971e

Please sign in to comment.