Update TypeScript

This commit is contained in:
Sindre Sorhus 2020-02-22 02:01:58 +07:00
parent 1ff389cabb
commit 05cdaccf92
4 changed files with 33 additions and 26 deletions

View file

@ -1,6 +1,6 @@
MIT License MIT License
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com) Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

View file

@ -8,7 +8,7 @@
"author": { "author": {
"name": "Sindre Sorhus", "name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com", "email": "sindresorhus@gmail.com",
"url": "sindresorhus.com" "url": "https://sindresorhus.com"
}, },
"main": "dist", "main": "dist",
"engines": { "engines": {
@ -48,27 +48,25 @@
], ],
"devDependencies": { "devDependencies": {
"@sindresorhus/tsconfig": "^0.7.0", "@sindresorhus/tsconfig": "^0.7.0",
"@types/jsdom": "^12.2.4", "@types/jsdom": "^16.1.0",
"@types/node": "^12.12.6", "@types/node": "^13.7.4",
"@types/zen-observable": "^0.8.0", "@types/zen-observable": "^0.8.0",
"@typescript-eslint/eslint-plugin": "^2.18.0", "@typescript-eslint/eslint-plugin": "^2.20.0",
"@typescript-eslint/parser": "^2.18.0", "@typescript-eslint/parser": "^2.20.0",
"ava": "^2.1.0", "ava": "^3.3.0",
"del-cli": "^2.0.0", "del-cli": "^2.0.0",
"eslint-config-xo-typescript": "^0.24.1", "eslint-config-xo-typescript": "^0.26.0",
"jsdom": "^16.0.1", "jsdom": "^16.0.1",
"rxjs": "^6.4.0", "rxjs": "^6.4.0",
"tempy": "^0.3.0", "tempy": "^0.4.0",
"ts-node": "^8.3.0", "ts-node": "^8.3.0",
"typescript": "^3.7.5", "typescript": "~3.8.2",
"xo": "^0.25.3", "xo": "^0.26.1",
"zen-observable": "^0.8.8" "zen-observable": "^0.8.8"
}, },
"types": "dist", "types": "dist",
"sideEffects": false, "sideEffects": false,
"ava": { "ava": {
"babel": false,
"compileEnhancements": false,
"extensions": [ "extensions": [
"ts" "ts"
], ],

View file

@ -133,13 +133,13 @@ is.numericString = (value: unknown): value is string =>
is.string(value) && value.length > 0 && !Number.isNaN(Number(value)); is.string(value) && value.length > 0 && !Number.isNaN(Number(value));
is.array = Array.isArray; is.array = Array.isArray;
is.buffer = (value: unknown): value is Buffer => !is.nullOrUndefined(value) && !is.nullOrUndefined((value as Buffer).constructor) && is.function_((value as Buffer).constructor.isBuffer) && (value as Buffer).constructor.isBuffer(value); is.buffer = (value: unknown): value is Buffer => (value as any)?.constructor?.isBuffer?.(value) ?? false;
is.nullOrUndefined = (value: unknown): value is null | undefined => is.null_(value) || is.undefined(value); is.nullOrUndefined = (value: unknown): value is null | undefined => is.null_(value) || is.undefined(value);
is.object = (value: unknown): value is object => !is.null_(value) && (typeof value === 'object' || is.function_(value)); is.object = (value: unknown): value is object => !is.null_(value) && (typeof value === 'object' || is.function_(value));
is.iterable = <T = unknown>(value: unknown): value is IterableIterator<T> => !is.nullOrUndefined(value) && is.function_((value as IterableIterator<T>)[Symbol.iterator]); is.iterable = <T = unknown>(value: unknown): value is IterableIterator<T> => is.function_((value as IterableIterator<T>)?.[Symbol.iterator]);
is.asyncIterable = <T = unknown>(value: unknown): value is AsyncIterableIterator<T> => !is.nullOrUndefined(value) && is.function_((value as AsyncIterableIterator<T>)[Symbol.asyncIterator]); is.asyncIterable = <T = unknown>(value: unknown): value is AsyncIterableIterator<T> => is.function_((value as AsyncIterableIterator<T>)?.[Symbol.asyncIterator]);
is.generator = (value: unknown): value is Generator => is.iterable(value) && is.function_(value.next) && is.function_(value.throw); is.generator = (value: unknown): value is Generator => is.iterable(value) && is.function_(value.next) && is.function_(value.throw);
@ -149,9 +149,8 @@ is.nativePromise = <T = unknown>(value: unknown): value is Promise<T> =>
isObjectOfType<Promise<T>>(TypeName.Promise)(value); isObjectOfType<Promise<T>>(TypeName.Promise)(value);
const hasPromiseAPI = <T = unknown>(value: unknown): value is Promise<T> => const hasPromiseAPI = <T = unknown>(value: unknown): value is Promise<T> =>
is.object(value) && is.function_((value as Promise<T>)?.then) &&
is.function_((value as Promise<T>).then) && // eslint-disable-line promise/prefer-await-to-then is.function_((value as Promise<T>)?.catch);
is.function_((value as Promise<T>).catch);
is.promise = <T = unknown>(value: unknown): value is Promise<T> => is.nativePromise(value) || hasPromiseAPI(value); is.promise = <T = unknown>(value: unknown): value is Promise<T> => is.nativePromise(value) || hasPromiseAPI(value);
@ -326,11 +325,11 @@ is.observable = (value: unknown): value is ObservableLike => {
} }
// eslint-disable-next-line no-use-extend-native/no-use-extend-native // eslint-disable-next-line no-use-extend-native/no-use-extend-native
if ((value as any)[Symbol.observable] && value === (value as any)[Symbol.observable]()) { if (value === (value as any)[Symbol.observable]?.()) {
return true; return true;
} }
if ((value as any)['@@observable'] && value === (value as any)['@@observable']()) { if (value === (value as any)['@@observable']?.()) {
return true; return true;
} }
@ -357,7 +356,7 @@ is.emptyString = (value: unknown): value is '' => is.string(value) && value.leng
// TODO: Use `not ''` when the `not` operator is available. // TODO: Use `not ''` when the `not` operator is available.
is.nonEmptyString = (value: unknown): value is string => is.string(value) && value.length > 0; is.nonEmptyString = (value: unknown): value is string => is.string(value) && value.length > 0;
const isWhiteSpaceString = (value: unknown): value is string => is.string(value) && /\S/.test(value) === false; const isWhiteSpaceString = (value: unknown): value is string => is.string(value) && !/\S/.test(value);
is.emptyStringOrWhitespace = (value: unknown): value is string => is.emptyString(value) || isWhiteSpaceString(value); is.emptyStringOrWhitespace = (value: unknown): value is string => is.emptyString(value) || isWhiteSpaceString(value);
is.emptyObject = <Key extends keyof any = string>(value: unknown): value is Record<Key, never> => is.object(value) && !is.map(value) && !is.set(value) && Object.keys(value).length === 0; is.emptyObject = <Key extends keyof any = string>(value: unknown): value is Record<Key, never> => is.object(value) && !is.map(value) && !is.set(value) && Object.keys(value).length === 0;
@ -377,7 +376,7 @@ export type Predicate = (value: unknown) => boolean;
type ArrayMethod = (fn: (value: unknown, index: number, array: unknown[]) => boolean, thisArg?: unknown) => boolean; type ArrayMethod = (fn: (value: unknown, index: number, array: unknown[]) => boolean, thisArg?: unknown) => boolean;
const predicateOnArray = (method: ArrayMethod, predicate: Predicate, values: unknown[]) => { const predicateOnArray = (method: ArrayMethod, predicate: Predicate, values: unknown[]) => {
if (is.function_(predicate) === false) { if (!is.function_(predicate)) {
throw new TypeError(`Invalid predicate: ${JSON.stringify(predicate)}`); throw new TypeError(`Invalid predicate: ${JSON.stringify(predicate)}`);
} }

View file

@ -571,13 +571,23 @@ const testType = (t: ExecutionContext, type: string, exclude?: string[]) => {
} }
const isTypeUnderTest = key === type; const isTypeUnderTest = key === type;
const assertIs = isTypeUnderTest ? t.true.bind(t) : t.false.bind(t); const assertIs = isTypeUnderTest ? t.true : t.false;
const assertAsserts = isTypeUnderTest ? t.notThrows.bind(t) : t.throws.bind(t);
for (const fixture of fixtures) { for (const fixture of fixtures) {
assertIs(testIs(fixture), `Value: ${inspect(fixture)}`); assertIs(testIs(fixture), `Value: ${inspect(fixture)}`);
const valueType = typeDescription ? typeDescription : typename; const valueType = typeDescription ? typeDescription : typename;
assertAsserts(() => testAssert(fixture), `Expected value which is \`${valueType!}\`, received value of type \`${is(fixture)}\`.`);
if (isTypeUnderTest) {
t.notThrows(() => {
testAssert(fixture);
});
} else {
t.throws(() => {
testAssert(fixture);
}, {
message: `Expected value which is \`${valueType!}\`, received value of type \`${is(fixture)}\`.`
});
}
if (isTypeUnderTest && typename) { if (isTypeUnderTest && typename) {
t.is(is(fixture), typename); t.is(is(fixture), typename);