-
-
Notifications
You must be signed in to change notification settings - Fork 546
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
51eb3f7
commit 0a098c6
Showing
12 changed files
with
148 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import type {RequireExactlyOne} from './require-exactly-one'; | ||
import type {RequireNone} from './internal'; | ||
|
||
/** | ||
Create a type that requires exactly one of the given keys and disallows more, or none of the given keys. The remaining keys are kept as is. | ||
@example | ||
``` | ||
import type {RequireOneOrNone} from 'type-fest'; | ||
type Responder = RequireOneOrNone<{ | ||
text: () => string; | ||
json: () => string; | ||
secure: boolean; | ||
}, 'text' | 'json'>; | ||
const responder1: Responder = { | ||
secure: true | ||
}; | ||
const responder2: Responder = { | ||
text: () => '{"message": "hi"}', | ||
secure: true | ||
}; | ||
const responder3: Responder = { | ||
json: () => '{"message": "ok"}', | ||
secure: true | ||
}; | ||
``` | ||
@category Object | ||
*/ | ||
export type RequireOneOrNone<ObjectType, KeysType extends keyof ObjectType = keyof ObjectType> = ( | ||
| RequireExactlyOne<ObjectType, KeysType> | ||
| RequireNone<KeysType> | ||
) & Omit<ObjectType, KeysType>; // Ignore unspecified keys. |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/* eslint-disable @typescript-eslint/no-duplicate-type-constituents */ | ||
import {expectType} from 'tsd'; | ||
import type {IsNotFalse} from '../../source/internal'; | ||
|
||
expectType<IsNotFalse<true>>(true); | ||
expectType<IsNotFalse<boolean>>(true); | ||
expectType<IsNotFalse<true | false>>(true); | ||
expectType<IsNotFalse<true | false | false | false>>(true); | ||
expectType<IsNotFalse<false>>(false); | ||
expectType<IsNotFalse<false | false>>(false); | ||
expectType<IsNotFalse<false | false | false | false>>(false); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import {expectType} from 'tsd'; | ||
import type {IsNull} from '../../source/internal'; | ||
|
||
// https://www.typescriptlang.org/docs/handbook/type-compatibility.html | ||
expectType<IsNull<null>>(true); | ||
expectType<IsNull<any>>(true); | ||
expectType<IsNull<never>>(true); | ||
expectType<IsNull<undefined>>(false); // Depends on `strictNullChecks` | ||
expectType<IsNull<unknown>>(false); | ||
expectType<IsNull<void>>(false); | ||
expectType<IsNull<{}>>(false); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import {expectType} from 'tsd'; | ||
import type {IsNumeric} from '../../source/internal'; | ||
|
||
expectType<IsNumeric<''>>(false); | ||
expectType<IsNumeric<'0'>>(true); | ||
expectType<IsNumeric<'1'>>(true); | ||
expectType<IsNumeric<'-1'>>(true); | ||
expectType<IsNumeric<'123'>>(true); | ||
expectType<IsNumeric<'1e2'>>(true); | ||
expectType<IsNumeric<'1.23'>>(true); | ||
expectType<IsNumeric<'123.456'>>(true); | ||
expectType<IsNumeric<'1.23e4'>>(true); | ||
expectType<IsNumeric<'1.23e-4'>>(true); | ||
expectType<IsNumeric<' '>>(false); | ||
expectType<IsNumeric<'\n'>>(false); | ||
expectType<IsNumeric<'\u{9}'>>(false); | ||
expectType<IsNumeric<' 1.2'>>(false); | ||
expectType<IsNumeric<'1 2'>>(false); | ||
expectType<IsNumeric<'1_200'>>(false); | ||
expectType<IsNumeric<' 1 '>>(false); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import {expectType} from 'tsd'; | ||
import type {IsWhitespace} from '../../source/internal'; | ||
|
||
expectType<IsWhitespace<''>>(false); | ||
expectType<IsWhitespace<' '>>(true); | ||
expectType<IsWhitespace<'\n'>>(true); | ||
expectType<IsWhitespace<'\u{9}'>>(true); | ||
expectType<IsWhitespace<'a'>>(false); | ||
expectType<IsWhitespace<'a '>>(false); | ||
expectType<IsWhitespace<' '>>(true); | ||
expectType<IsWhitespace<' \t '>>(true); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import {expectAssignable, expectNotAssignable, expectType} from 'tsd'; | ||
import {type RequireNone} from '../../source/internal'; | ||
|
||
type NoneAllowed = RequireNone<'foo' | 'bar'>; | ||
|
||
expectAssignable<NoneAllowed>({}); | ||
expectNotAssignable<NoneAllowed>({foo: 'foo'}); | ||
expectNotAssignable<NoneAllowed>({bar: 'bar'}); | ||
expectNotAssignable<NoneAllowed>({foo: 'foo', bar: 'bar'}); | ||
|
||
type SomeAllowed = Record<'bar', string> & RequireNone<'foo'>; | ||
|
||
expectAssignable<SomeAllowed>({bar: 'bar'}); | ||
expectNotAssignable<SomeAllowed>({foo: 'foo'}); | ||
expectNotAssignable<SomeAllowed>({foo: 'foo', bar: 'bar'}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import {expectAssignable, expectNotAssignable} from 'tsd'; | ||
import type {RequireOneOrNone} from '../index'; | ||
|
||
type OneAtMost = RequireOneOrNone<Record<'foo' | 'bar' | 'baz', true>>; | ||
|
||
expectAssignable<OneAtMost>({}); | ||
expectAssignable<OneAtMost>({foo: true}); | ||
expectAssignable<OneAtMost>({bar: true}); | ||
expectAssignable<OneAtMost>({baz: true}); | ||
|
||
expectNotAssignable<OneAtMost>({foo: true, bar: true}); | ||
expectNotAssignable<OneAtMost>({foo: true, baz: true}); | ||
expectNotAssignable<OneAtMost>({bar: true, baz: true}); | ||
expectNotAssignable<OneAtMost>({foo: true, bar: true, baz: true}); | ||
|
||
// 'foo' always required | ||
type OneOrTwo = RequireOneOrNone<Record<'foo' | 'bar' | 'baz', true>, 'bar' | 'baz'>; | ||
|
||
expectAssignable<OneOrTwo>({foo: true}); | ||
expectAssignable<OneOrTwo>({foo: true, bar: true}); | ||
expectAssignable<OneOrTwo>({foo: true, baz: true}); | ||
|
||
expectNotAssignable<OneOrTwo>({}); | ||
expectNotAssignable<OneOrTwo>({bar: true}); | ||
expectNotAssignable<OneOrTwo>({baz: true}); | ||
expectNotAssignable<OneOrTwo>({foo: true, bar: true, baz: true}); |