Skip to content

Commit

Permalink
feat: 为 types 工具添加测试
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Nov 20, 2020
1 parent 8967108 commit 26be1f6
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 5 deletions.
5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,7 @@
"require": "./_cjs/react/index.js",
"import": "./react/index.js"
},
"./types": {
"require": "./_cjs/types/index.d.ts",
"import": "./types/index.d.ts"
},
"./types": "./types/index.d.ts",
"./utils": {
"require": "./_cjs/utils/index.js",
"import": "./utils/index.js"
Expand Down
14 changes: 14 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions src/types/Defined.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Defined } from './Defined'
import { expectType } from './__expectType__'

describe('Defined', () => {
test('表现正常', () => {
expectType<Defined<number | undefined>, number>()
expectType<number | undefined, undefined>()
// @ts-expect-error
expectType<Defined<number | undefined>, undefined>()
})
})
10 changes: 10 additions & 0 deletions src/types/FirstParameter.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { expectType } from './__expectType__'
import { FirstParameter } from './FirstParameter'

describe('FirstParameter', () => {
test('表现正常', () => {
expectType<FirstParameter<(id: number) => any>, number>()
// @ts-expect-error
expectType<FirstParameter<(id: number) => any>, string>()
})
})
20 changes: 20 additions & 0 deletions src/types/IsAny.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { expectType } from './__expectType__'
import { IsAny } from './IsAny'

describe('IsAny', () => {
test('不是 any', () => {
expectType<IsAny<number>, false>()
expectType<IsAny<boolean>, false>()
expectType<IsAny<string>, false>()
expectType<IsAny<RegExp>, false>()
expectType<IsAny<undefined>, false>()
expectType<IsAny<null>, false>()
expectType<IsAny<unknown>, false>()
expectType<IsAny<never>, false>()
expectType<IsAny<any[]>, false>()
})

test('是 any', () => {
expectType<IsAny<any>, true>()
})
})
20 changes: 20 additions & 0 deletions src/types/IsNever.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { expectType } from './__expectType__'
import { IsNever } from './IsNever'

describe('IsNever', () => {
test('不是 never', () => {
expectType<IsNever<number>, false>()
expectType<IsNever<boolean>, false>()
expectType<IsNever<string>, false>()
expectType<IsNever<RegExp>, false>()
expectType<IsNever<undefined>, false>()
expectType<IsNever<null>, false>()
expectType<IsNever<unknown>, false>()
expectType<IsNever<any>, false>()
expectType<IsNever<any[]>, false>()
})

test('是 never', () => {
expectType<IsNever<never>, true>()
})
})
13 changes: 13 additions & 0 deletions src/types/OneOrMore.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { expectType } from './__expectType__'
import { OneOrMore } from './OneOrMore'

describe('OneOrMore', () => {
test('表现正常', () => {
expectType<OneOrMore<number>, number>()
expectType<OneOrMore<number>, number[]>()
expectType<OneOrMore<number>, []>()
expectType<OneOrMore<number>, 1>()
// @ts-expect-error
expectType<OneOrMore<number>, string>()
})
})
3 changes: 3 additions & 0 deletions src/types/__expectType__.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function expectType<X, Y extends X>() {
expect(1).toBe(1)
}
2 changes: 1 addition & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export type {
RequiredKeys,
} from 'ts-essentials'

// @index(['./**/*.ts', '!./**/*.test.*'], f => `export * from '${f.path}'`)
// @index(['./**/*.ts', '!./**/*.test.*', '!**/__*'], f => `export * from '${f.path}'`)
export * from './AnyAsyncFunction'
export * from './AnyFunction'
export * from './AnyObject'
Expand Down

0 comments on commit 26be1f6

Please sign in to comment.