mirror of
https://github.com/denoland/std.git
synced 2024-11-22 04:59:05 +00:00
chore: remove _util/assert_type.ts
(#2952)
This commit is contained in:
parent
294754176f
commit
8e46f5953e
@ -1,37 +0,0 @@
|
||||
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
||||
// Original author: @dsherret https://github.com/dsherret/conditional-type-checks
|
||||
|
||||
/**
|
||||
* Asserts at compile time that the provided type argument's type resolves to the expected boolean literal type.
|
||||
* @param _expectTrue - True if the passed in type argument resolved to true.
|
||||
*/
|
||||
export function assertType<T extends true | false>(_expectTrue: T) {}
|
||||
|
||||
/**
|
||||
* Checks if type `T` exactly matches type `U`.
|
||||
*/
|
||||
export type IsExact<T, U> = TupleMatches<AnyToBrand<T>, AnyToBrand<U>> extends
|
||||
true ? TupleMatches<
|
||||
DeepMakeRequiredForIsExact<T>,
|
||||
DeepMakeRequiredForIsExact<U>
|
||||
> extends true // catch optional properties
|
||||
? true
|
||||
: false
|
||||
: false;
|
||||
|
||||
type DeepMakeRequiredForIsExact<T> = {
|
||||
[P in keyof T]-?: DeepMakeRequiredForIsExact<AnyToBrand<T[P]>>;
|
||||
};
|
||||
|
||||
/**
|
||||
* Checks if type `T` is the `any` type.
|
||||
*/
|
||||
// https://stackoverflow.com/a/49928360/3406963
|
||||
export type IsAny<T> = 0 extends (1 & T) ? true : false;
|
||||
|
||||
type TupleMatches<T, U> = Matches<[T], [U]> extends true ? true : false;
|
||||
type Matches<T, U> = T extends U ? U extends T ? true : false : false;
|
||||
|
||||
type AnyToBrand<T> = IsAny<T> extends true
|
||||
? { __conditionalTypeChecksAny__: undefined }
|
||||
: T;
|
@ -1,85 +0,0 @@
|
||||
// deno-lint-ignore-file no-explicit-any
|
||||
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
||||
// Original author: @dsherret https://github.com/dsherret/conditional-type-checks
|
||||
|
||||
import { assertType, IsAny, IsExact } from "./assert_type.ts";
|
||||
|
||||
// IsExact
|
||||
{
|
||||
// matching
|
||||
assertType<IsExact<string | number, string | number>>(true);
|
||||
assertType<IsExact<string | number | Date, string | number | Date>>(true);
|
||||
assertType<IsExact<string | undefined, string | undefined>>(true);
|
||||
assertType<IsExact<any, any>>(true); // ok to have any for both
|
||||
assertType<IsExact<unknown, unknown>>(true);
|
||||
assertType<IsExact<never, never>>(true);
|
||||
// deno-lint-ignore ban-types
|
||||
assertType<IsExact<{}, {}>>(true);
|
||||
assertType<IsExact<{ prop: string }, { prop: string }>>(true);
|
||||
assertType<IsExact<{ prop: { prop: string } }, { prop: { prop: string } }>>(
|
||||
true,
|
||||
);
|
||||
assertType<IsExact<{ prop: never }, { prop: never }>>(true);
|
||||
assertType<IsExact<{ prop: any }, { prop: any }>>(true);
|
||||
assertType<IsExact<{ prop: unknown }, { prop: unknown }>>(true);
|
||||
assertType<IsExact<Window, Window>>(true);
|
||||
|
||||
// not matching
|
||||
assertType<IsExact<string | number | Date, string | number>>(false);
|
||||
assertType<IsExact<string, string | number>>(false);
|
||||
assertType<IsExact<string | undefined, string>>(false);
|
||||
assertType<IsExact<string | undefined, any | string>>(false);
|
||||
assertType<IsExact<any | string | undefined, string>>(false);
|
||||
assertType<IsExact<string, any>>(false);
|
||||
assertType<IsExact<string, unknown>>(false);
|
||||
assertType<IsExact<string, never>>(false);
|
||||
assertType<IsExact<never, never | string>>(false);
|
||||
assertType<IsExact<unknown, any>>(false);
|
||||
assertType<IsExact<never, any>>(false);
|
||||
assertType<IsExact<Date | Window, Date>>(false);
|
||||
assertType<IsExact<{ name: string; other?: Date }, { name: string }>>(false);
|
||||
assertType<IsExact<{ prop: Date }, { prop: string }>>(false);
|
||||
assertType<IsExact<{ other?: Date }, { prop?: string }>>(false);
|
||||
assertType<IsExact<{ prop: { prop?: string } }, { prop: { prop: string } }>>(
|
||||
false,
|
||||
);
|
||||
assertType<IsExact<{ prop: any }, { prop: string }>>(false);
|
||||
assertType<IsExact<{ prop: any }, { prop: unknown }>>(false);
|
||||
assertType<IsExact<{ prop: any }, { prop: never }>>(false);
|
||||
assertType<IsExact<{ prop: unknown }, { prop: never }>>(false);
|
||||
assertType<IsExact<{ prop: { prop: unknown } }, { prop: { prop: any } }>>(
|
||||
false,
|
||||
);
|
||||
assertType<IsExact<{ prop: { prop: unknown } }, { prop: { prop: never } }>>(
|
||||
false,
|
||||
);
|
||||
assertType<IsExact<{ prop: { prop: any } }, { prop: { prop: never } }>>(
|
||||
false,
|
||||
);
|
||||
assertType<IsExact<{ prop: string }, { prop: never }>>(false);
|
||||
assertType<IsExact<{ prop: { prop: any } }, { prop: { prop: string } }>>(
|
||||
false,
|
||||
);
|
||||
assertType<
|
||||
IsExact<
|
||||
{ prop: any } | { prop: string },
|
||||
{ prop: number } | { prop: string }
|
||||
>
|
||||
>(false);
|
||||
assertType<IsExact<{ prop: string | undefined }, { prop?: string }>>(false); // these are different
|
||||
}
|
||||
|
||||
// IsAny
|
||||
{
|
||||
// matching
|
||||
assertType<IsAny<any>>(true);
|
||||
|
||||
// not matching
|
||||
assertType<IsAny<string>>(false);
|
||||
assertType<IsAny<unknown>>(false);
|
||||
assertType<IsAny<never>>(false);
|
||||
|
||||
// tests for issue #3 (IsAny resolving to boolean)
|
||||
assertType<IsExact<IsAny<string>, false>>(true);
|
||||
assertType<IsExact<IsAny<5>, false>>(true);
|
||||
}
|
@ -15,8 +15,8 @@ import {
|
||||
stringify,
|
||||
} from "./mod.ts";
|
||||
import * as path from "../path/mod.ts";
|
||||
import type { IsExact } from "../_util/assert_type.ts";
|
||||
import { assertType } from "../_util/assert_type.ts";
|
||||
import type { IsExact } from "../testing/types.ts";
|
||||
import { assertType } from "../testing/types.ts";
|
||||
|
||||
const moduleDir = path.dirname(path.fromFileUrl(import.meta.url));
|
||||
const testdataDir = path.resolve(moduleDir, "testdata");
|
||||
|
@ -1,7 +1,7 @@
|
||||
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
||||
import { assertEquals } from "../testing/asserts.ts";
|
||||
import { Args, parse, ParseOptions } from "./mod.ts";
|
||||
import { assertType, IsExact } from "../_util/assert_type.ts";
|
||||
import { assertType, IsExact } from "../testing/types.ts";
|
||||
|
||||
// flag boolean true (default all --args to boolean)
|
||||
Deno.test("flagBooleanTrue", function () {
|
||||
|
Loading…
Reference in New Issue
Block a user