From d8a29c19f2f622b784bfa36209c8f68bd19edd69 Mon Sep 17 00:00:00 2001 From: andy Date: Sun, 21 Jul 2019 20:06:39 -0600 Subject: [PATCH 1/4] feat: remove the `string` condition on `IsNever` Thanks to @krisdages for finding this! This commit removes the `string` condition on `IsNever` because `keyof never === keyof any === string`. Closes: #109 --- src/types/predicates.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/types/predicates.ts b/src/types/predicates.ts index b2731b6..2eeb176 100644 --- a/src/types/predicates.ts +++ b/src/types/predicates.ts @@ -1,4 +1,4 @@ -import { False, True, And, Or, Not } from './conditionals'; +import { False, True, And, Or, Not, If } from './conditionals'; /** no-doc */ export type KnownProblemPrototypeKeys = 'toString' | 'toLocaleString' | 'hasOwnProperty' | 'isPrototypeOf' | 'propertyIsEnumerable' | 'constructor' | 'valueOf'; @@ -15,7 +15,7 @@ export type ObjectPrototypeKeys = keyof Object; /** no-doc */ export type FunctionPrototypeKeys = keyof Function; -export type IsNever = Not<(Record & Record)[S]>; +export type IsNever = keyof any extends keyof T ? If, False, True> : False; export type IsType = X extends T ? True : False; export type IsArray = T extends unknown[] ? True : False; export type IsNumber = T extends number ? True : False; From 62f37613133ae9312aea0b95bd9d1abf8619c2f8 Mon Sep 17 00:00:00 2001 From: andy Date: Sun, 21 Jul 2019 20:15:04 -0600 Subject: [PATCH 2/4] test: simplify conditional and add tests The tests for this were purely focused around the string form. This commit adds more complete tests to account for the wider range of applicability. This commit also simplifies the conditional of the original form. --- src/types/predicates.ts | 4 ++-- test/predicates/IsNever.test.ts | 20 ++++++++++++++++++++ test/strings/IsNever.test.ts | 11 ----------- 3 files changed, 22 insertions(+), 13 deletions(-) create mode 100644 test/predicates/IsNever.test.ts delete mode 100644 test/strings/IsNever.test.ts diff --git a/src/types/predicates.ts b/src/types/predicates.ts index 2eeb176..2ecb62b 100644 --- a/src/types/predicates.ts +++ b/src/types/predicates.ts @@ -1,4 +1,4 @@ -import { False, True, And, Or, Not, If } from './conditionals'; +import { False, True, And, Or, Not } from './conditionals'; /** no-doc */ export type KnownProblemPrototypeKeys = 'toString' | 'toLocaleString' | 'hasOwnProperty' | 'isPrototypeOf' | 'propertyIsEnumerable' | 'constructor' | 'valueOf'; @@ -15,7 +15,7 @@ export type ObjectPrototypeKeys = keyof Object; /** no-doc */ export type FunctionPrototypeKeys = keyof Function; -export type IsNever = keyof any extends keyof T ? If, False, True> : False; +export type IsNever = keyof any extends keyof T ? Not> : False; export type IsType = X extends T ? True : False; export type IsArray = T extends unknown[] ? True : False; export type IsNumber = T extends number ? True : False; diff --git a/test/predicates/IsNever.test.ts b/test/predicates/IsNever.test.ts new file mode 100644 index 0000000..8e05445 --- /dev/null +++ b/test/predicates/IsNever.test.ts @@ -0,0 +1,20 @@ +import test from 'ava'; +import { assert } from '../helpers/assert'; + +import { False, True, IsNever } from '../../src'; + +test('Can ask if T is of type "never"', t => { + assert, False>(t); + assert, False>(t); + assert, False>(t); + assert, False>(t); + assert string>, False>(t); + assert, False>(t); + assert, False>(t); + assert, False>(t); + assert, False>(t); + assert, False>(t); + assert, False>(t); + + assert, True>(t); +}); diff --git a/test/strings/IsNever.test.ts b/test/strings/IsNever.test.ts deleted file mode 100644 index ce6adb7..0000000 --- a/test/strings/IsNever.test.ts +++ /dev/null @@ -1,11 +0,0 @@ -import test from 'ava'; -import { assert } from '../helpers/assert'; - -import { False, True, IsNever } from '../../src'; - -test('Can ask if a string is of type "never"', t => { - type str = 'hi'; - assert, False>(t); - assert, True>(t); - assert, True>(t); -}); From acae02b8cfb62f25255618a41668d2eb227e3b93 Mon Sep 17 00:00:00 2001 From: andy Date: Sun, 21 Jul 2019 20:16:23 -0600 Subject: [PATCH 3/4] docs: generate documentation --- README.md | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index bdc861b..5005413 100644 --- a/README.md +++ b/README.md @@ -949,7 +949,23 @@ test('Conditions can be based on XOR', t => { ### IsNever - +```ts +test('Can ask if T is of type "never"', t => { + assert, False>(t); + assert, False>(t); + assert, False>(t); + assert, False>(t); + assert string>, False>(t); + assert, False>(t); + assert, False>(t); + assert, False>(t); + assert, False>(t); + assert, False>(t); + assert, False>(t); + + assert, True>(t); +}); +``` ### IsNil From 2c0afc80260cd3d1a897793e40133f8a2ab0a8d6 Mon Sep 17 00:00:00 2001 From: andy Date: Sun, 21 Jul 2019 20:52:26 -0600 Subject: [PATCH 4/4] test: drop older versions of typescript The new `IsNever` only works with 3.5, so all older versions need to be dropped. This will bump the major version of `simplytyped` to avoid any breaking changes. I think it is worth it to drop older versions of the language to keep this package from rotting too severely, as long as we are careful with updating the major version whenever we change the supported TS versions. BREAKING CHANGE: no longer supporting typescript < 3.5 --- scripts/testTsVersions.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/testTsVersions.sh b/scripts/testTsVersions.sh index d6c1bac..ab538d1 100644 --- a/scripts/testTsVersions.sh +++ b/scripts/testTsVersions.sh @@ -1,6 +1,6 @@ set -e -for v in 3.0.3 3.1.6 3.2.2 next; do +for v in 3.5.3 next; do npm install --no-save typescript@$v npm test done