@eslint-react/shared 1.38.0-next.5 → 1.38.0-next.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +526 -9
- package/dist/index.d.ts +526 -9
- package/package.json +5 -5
package/dist/index.d.mts
CHANGED
|
@@ -2094,11 +2094,520 @@ declare global {
|
|
|
2094
2094
|
}
|
|
2095
2095
|
}
|
|
2096
2096
|
|
|
2097
|
+
/**
|
|
2098
|
+
Extract all required keys from the given type.
|
|
2099
|
+
|
|
2100
|
+
This is useful when you want to create a new type that contains different type values for the required keys only or use the list of keys for validation purposes, etc...
|
|
2101
|
+
|
|
2102
|
+
@example
|
|
2103
|
+
```
|
|
2104
|
+
import type {RequiredKeysOf} from 'type-fest';
|
|
2105
|
+
|
|
2106
|
+
declare function createValidation<Entity extends object, Key extends RequiredKeysOf<Entity> = RequiredKeysOf<Entity>>(field: Key, validator: (value: Entity[Key]) => boolean): ValidatorFn;
|
|
2107
|
+
|
|
2108
|
+
interface User {
|
|
2109
|
+
name: string;
|
|
2110
|
+
surname: string;
|
|
2111
|
+
|
|
2112
|
+
luckyNumber?: number;
|
|
2113
|
+
}
|
|
2114
|
+
|
|
2115
|
+
const validator1 = createValidation<User>('name', value => value.length < 25);
|
|
2116
|
+
const validator2 = createValidation<User>('surname', value => value.length < 25);
|
|
2117
|
+
```
|
|
2118
|
+
|
|
2119
|
+
@category Utilities
|
|
2120
|
+
*/
|
|
2121
|
+
type RequiredKeysOf<BaseType extends object> = Exclude<{
|
|
2122
|
+
[Key in keyof BaseType]: BaseType extends Record<Key, BaseType[Key]>
|
|
2123
|
+
? Key
|
|
2124
|
+
: never
|
|
2125
|
+
}[keyof BaseType], undefined>;
|
|
2126
|
+
|
|
2127
|
+
/**
|
|
2128
|
+
Returns a boolean for whether the given type is `never`.
|
|
2129
|
+
|
|
2130
|
+
@link https://github.com/microsoft/TypeScript/issues/31751#issuecomment-498526919
|
|
2131
|
+
@link https://stackoverflow.com/a/53984913/10292952
|
|
2132
|
+
@link https://www.zhenghao.io/posts/ts-never
|
|
2133
|
+
|
|
2134
|
+
Useful in type utilities, such as checking if something does not occur.
|
|
2135
|
+
|
|
2136
|
+
@example
|
|
2137
|
+
```
|
|
2138
|
+
import type {IsNever, And} from 'type-fest';
|
|
2139
|
+
|
|
2140
|
+
// https://github.com/andnp/SimplyTyped/blob/master/src/types/strings.ts
|
|
2141
|
+
type AreStringsEqual<A extends string, B extends string> =
|
|
2142
|
+
And<
|
|
2143
|
+
IsNever<Exclude<A, B>> extends true ? true : false,
|
|
2144
|
+
IsNever<Exclude<B, A>> extends true ? true : false
|
|
2145
|
+
>;
|
|
2146
|
+
|
|
2147
|
+
type EndIfEqual<I extends string, O extends string> =
|
|
2148
|
+
AreStringsEqual<I, O> extends true
|
|
2149
|
+
? never
|
|
2150
|
+
: void;
|
|
2151
|
+
|
|
2152
|
+
function endIfEqual<I extends string, O extends string>(input: I, output: O): EndIfEqual<I, O> {
|
|
2153
|
+
if (input === output) {
|
|
2154
|
+
process.exit(0);
|
|
2155
|
+
}
|
|
2156
|
+
}
|
|
2157
|
+
|
|
2158
|
+
endIfEqual('abc', 'abc');
|
|
2159
|
+
//=> never
|
|
2160
|
+
|
|
2161
|
+
endIfEqual('abc', '123');
|
|
2162
|
+
//=> void
|
|
2163
|
+
```
|
|
2164
|
+
|
|
2165
|
+
@category Type Guard
|
|
2166
|
+
@category Utilities
|
|
2167
|
+
*/
|
|
2168
|
+
type IsNever<T> = [T] extends [never] ? true : false;
|
|
2169
|
+
|
|
2170
|
+
/**
|
|
2171
|
+
An if-else-like type that resolves depending on whether the given type is `never`.
|
|
2172
|
+
|
|
2173
|
+
@see {@link IsNever}
|
|
2174
|
+
|
|
2175
|
+
@example
|
|
2176
|
+
```
|
|
2177
|
+
import type {IfNever} from 'type-fest';
|
|
2178
|
+
|
|
2179
|
+
type ShouldBeTrue = IfNever<never>;
|
|
2180
|
+
//=> true
|
|
2181
|
+
|
|
2182
|
+
type ShouldBeBar = IfNever<'not never', 'foo', 'bar'>;
|
|
2183
|
+
//=> 'bar'
|
|
2184
|
+
```
|
|
2185
|
+
|
|
2186
|
+
@category Type Guard
|
|
2187
|
+
@category Utilities
|
|
2188
|
+
*/
|
|
2189
|
+
type IfNever<T, TypeIfNever = true, TypeIfNotNever = false> = (
|
|
2190
|
+
IsNever<T> extends true ? TypeIfNever : TypeIfNotNever
|
|
2191
|
+
);
|
|
2192
|
+
|
|
2193
|
+
// Can eventually be replaced with the built-in once this library supports
|
|
2194
|
+
// TS5.4+ only. Tracked in https://github.com/sindresorhus/type-fest/issues/848
|
|
2195
|
+
type NoInfer<T> = T extends infer U ? U : never;
|
|
2196
|
+
|
|
2197
|
+
/**
|
|
2198
|
+
Returns a boolean for whether the given type is `any`.
|
|
2199
|
+
|
|
2200
|
+
@link https://stackoverflow.com/a/49928360/1490091
|
|
2201
|
+
|
|
2202
|
+
Useful in type utilities, such as disallowing `any`s to be passed to a function.
|
|
2203
|
+
|
|
2204
|
+
@example
|
|
2205
|
+
```
|
|
2206
|
+
import type {IsAny} from 'type-fest';
|
|
2207
|
+
|
|
2208
|
+
const typedObject = {a: 1, b: 2} as const;
|
|
2209
|
+
const anyObject: any = {a: 1, b: 2};
|
|
2210
|
+
|
|
2211
|
+
function get<O extends (IsAny<O> extends true ? {} : Record<string, number>), K extends keyof O = keyof O>(obj: O, key: K) {
|
|
2212
|
+
return obj[key];
|
|
2213
|
+
}
|
|
2214
|
+
|
|
2215
|
+
const typedA = get(typedObject, 'a');
|
|
2216
|
+
//=> 1
|
|
2217
|
+
|
|
2218
|
+
const anyA = get(anyObject, 'a');
|
|
2219
|
+
//=> any
|
|
2220
|
+
```
|
|
2221
|
+
|
|
2222
|
+
@category Type Guard
|
|
2223
|
+
@category Utilities
|
|
2224
|
+
*/
|
|
2225
|
+
type IsAny<T> = 0 extends 1 & NoInfer<T> ? true : false;
|
|
2226
|
+
|
|
2227
|
+
/**
|
|
2228
|
+
Useful to flatten the type output to improve type hints shown in editors. And also to transform an interface into a type to aide with assignability.
|
|
2229
|
+
|
|
2230
|
+
@example
|
|
2231
|
+
```
|
|
2232
|
+
import type {Simplify} from 'type-fest';
|
|
2233
|
+
|
|
2234
|
+
type PositionProps = {
|
|
2235
|
+
top: number;
|
|
2236
|
+
left: number;
|
|
2237
|
+
};
|
|
2238
|
+
|
|
2239
|
+
type SizeProps = {
|
|
2240
|
+
width: number;
|
|
2241
|
+
height: number;
|
|
2242
|
+
};
|
|
2243
|
+
|
|
2244
|
+
// In your editor, hovering over `Props` will show a flattened object with all the properties.
|
|
2245
|
+
type Props = Simplify<PositionProps & SizeProps>;
|
|
2246
|
+
```
|
|
2247
|
+
|
|
2248
|
+
Sometimes it is desired to pass a value as a function argument that has a different type. At first inspection it may seem assignable, and then you discover it is not because the `value`'s type definition was defined as an interface. In the following example, `fn` requires an argument of type `Record<string, unknown>`. If the value is defined as a literal, then it is assignable. And if the `value` is defined as type using the `Simplify` utility the value is assignable. But if the `value` is defined as an interface, it is not assignable because the interface is not sealed and elsewhere a non-string property could be added to the interface.
|
|
2249
|
+
|
|
2250
|
+
If the type definition must be an interface (perhaps it was defined in a third-party npm package), then the `value` can be defined as `const value: Simplify<SomeInterface> = ...`. Then `value` will be assignable to the `fn` argument. Or the `value` can be cast as `Simplify<SomeInterface>` if you can't re-declare the `value`.
|
|
2251
|
+
|
|
2252
|
+
@example
|
|
2253
|
+
```
|
|
2254
|
+
import type {Simplify} from 'type-fest';
|
|
2255
|
+
|
|
2256
|
+
interface SomeInterface {
|
|
2257
|
+
foo: number;
|
|
2258
|
+
bar?: string;
|
|
2259
|
+
baz: number | undefined;
|
|
2260
|
+
}
|
|
2261
|
+
|
|
2262
|
+
type SomeType = {
|
|
2263
|
+
foo: number;
|
|
2264
|
+
bar?: string;
|
|
2265
|
+
baz: number | undefined;
|
|
2266
|
+
};
|
|
2267
|
+
|
|
2268
|
+
const literal = {foo: 123, bar: 'hello', baz: 456};
|
|
2269
|
+
const someType: SomeType = literal;
|
|
2270
|
+
const someInterface: SomeInterface = literal;
|
|
2271
|
+
|
|
2272
|
+
function fn(object: Record<string, unknown>): void {}
|
|
2273
|
+
|
|
2274
|
+
fn(literal); // Good: literal object type is sealed
|
|
2275
|
+
fn(someType); // Good: type is sealed
|
|
2276
|
+
fn(someInterface); // Error: Index signature for type 'string' is missing in type 'someInterface'. Because `interface` can be re-opened
|
|
2277
|
+
fn(someInterface as Simplify<SomeInterface>); // Good: transform an `interface` into a `type`
|
|
2278
|
+
```
|
|
2279
|
+
|
|
2280
|
+
@link https://github.com/microsoft/TypeScript/issues/15300
|
|
2281
|
+
@see SimplifyDeep
|
|
2282
|
+
@category Object
|
|
2283
|
+
*/
|
|
2284
|
+
type Simplify<T> = {[KeyType in keyof T]: T[KeyType]} & {};
|
|
2285
|
+
|
|
2286
|
+
/**
|
|
2287
|
+
Omit any index signatures from the given object type, leaving only explicitly defined properties.
|
|
2288
|
+
|
|
2289
|
+
This is the counterpart of `PickIndexSignature`.
|
|
2290
|
+
|
|
2291
|
+
Use-cases:
|
|
2292
|
+
- Remove overly permissive signatures from third-party types.
|
|
2293
|
+
|
|
2294
|
+
This type was taken from this [StackOverflow answer](https://stackoverflow.com/a/68261113/420747).
|
|
2295
|
+
|
|
2296
|
+
It relies on the fact that an empty object (`{}`) is assignable to an object with just an index signature, like `Record<string, unknown>`, but not to an object with explicitly defined keys, like `Record<'foo' | 'bar', unknown>`.
|
|
2297
|
+
|
|
2298
|
+
(The actual value type, `unknown`, is irrelevant and could be any type. Only the key type matters.)
|
|
2299
|
+
|
|
2300
|
+
```
|
|
2301
|
+
const indexed: Record<string, unknown> = {}; // Allowed
|
|
2302
|
+
|
|
2303
|
+
const keyed: Record<'foo', unknown> = {}; // Error
|
|
2304
|
+
// => TS2739: Type '{}' is missing the following properties from type 'Record<"foo" | "bar", unknown>': foo, bar
|
|
2305
|
+
```
|
|
2306
|
+
|
|
2307
|
+
Instead of causing a type error like the above, you can also use a [conditional type](https://www.typescriptlang.org/docs/handbook/2/conditional-types.html) to test whether a type is assignable to another:
|
|
2308
|
+
|
|
2309
|
+
```
|
|
2310
|
+
type Indexed = {} extends Record<string, unknown>
|
|
2311
|
+
? '✅ `{}` is assignable to `Record<string, unknown>`'
|
|
2312
|
+
: '❌ `{}` is NOT assignable to `Record<string, unknown>`';
|
|
2313
|
+
// => '✅ `{}` is assignable to `Record<string, unknown>`'
|
|
2314
|
+
|
|
2315
|
+
type Keyed = {} extends Record<'foo' | 'bar', unknown>
|
|
2316
|
+
? "✅ `{}` is assignable to `Record<'foo' | 'bar', unknown>`"
|
|
2317
|
+
: "❌ `{}` is NOT assignable to `Record<'foo' | 'bar', unknown>`";
|
|
2318
|
+
// => "❌ `{}` is NOT assignable to `Record<'foo' | 'bar', unknown>`"
|
|
2319
|
+
```
|
|
2320
|
+
|
|
2321
|
+
Using a [mapped type](https://www.typescriptlang.org/docs/handbook/2/mapped-types.html#further-exploration), you can then check for each `KeyType` of `ObjectType`...
|
|
2322
|
+
|
|
2323
|
+
```
|
|
2324
|
+
import type {OmitIndexSignature} from 'type-fest';
|
|
2325
|
+
|
|
2326
|
+
type OmitIndexSignature<ObjectType> = {
|
|
2327
|
+
[KeyType in keyof ObjectType // Map each key of `ObjectType`...
|
|
2328
|
+
]: ObjectType[KeyType]; // ...to its original value, i.e. `OmitIndexSignature<Foo> == Foo`.
|
|
2329
|
+
};
|
|
2330
|
+
```
|
|
2331
|
+
|
|
2332
|
+
...whether an empty object (`{}`) would be assignable to an object with that `KeyType` (`Record<KeyType, unknown>`)...
|
|
2333
|
+
|
|
2334
|
+
```
|
|
2335
|
+
import type {OmitIndexSignature} from 'type-fest';
|
|
2336
|
+
|
|
2337
|
+
type OmitIndexSignature<ObjectType> = {
|
|
2338
|
+
[KeyType in keyof ObjectType
|
|
2339
|
+
// Is `{}` assignable to `Record<KeyType, unknown>`?
|
|
2340
|
+
as {} extends Record<KeyType, unknown>
|
|
2341
|
+
? ... // ✅ `{}` is assignable to `Record<KeyType, unknown>`
|
|
2342
|
+
: ... // ❌ `{}` is NOT assignable to `Record<KeyType, unknown>`
|
|
2343
|
+
]: ObjectType[KeyType];
|
|
2344
|
+
};
|
|
2345
|
+
```
|
|
2346
|
+
|
|
2347
|
+
If `{}` is assignable, it means that `KeyType` is an index signature and we want to remove it. If it is not assignable, `KeyType` is a "real" key and we want to keep it.
|
|
2348
|
+
|
|
2349
|
+
@example
|
|
2350
|
+
```
|
|
2351
|
+
import type {OmitIndexSignature} from 'type-fest';
|
|
2352
|
+
|
|
2353
|
+
interface Example {
|
|
2354
|
+
// These index signatures will be removed.
|
|
2355
|
+
[x: string]: any
|
|
2356
|
+
[x: number]: any
|
|
2357
|
+
[x: symbol]: any
|
|
2358
|
+
[x: `head-${string}`]: string
|
|
2359
|
+
[x: `${string}-tail`]: string
|
|
2360
|
+
[x: `head-${string}-tail`]: string
|
|
2361
|
+
[x: `${bigint}`]: string
|
|
2362
|
+
[x: `embedded-${number}`]: string
|
|
2363
|
+
|
|
2364
|
+
// These explicitly defined keys will remain.
|
|
2365
|
+
foo: 'bar';
|
|
2366
|
+
qux?: 'baz';
|
|
2367
|
+
}
|
|
2368
|
+
|
|
2369
|
+
type ExampleWithoutIndexSignatures = OmitIndexSignature<Example>;
|
|
2370
|
+
// => { foo: 'bar'; qux?: 'baz' | undefined; }
|
|
2371
|
+
```
|
|
2372
|
+
|
|
2373
|
+
@see PickIndexSignature
|
|
2374
|
+
@category Object
|
|
2375
|
+
*/
|
|
2376
|
+
type OmitIndexSignature<ObjectType> = {
|
|
2377
|
+
[KeyType in keyof ObjectType as {} extends Record<KeyType, unknown>
|
|
2378
|
+
? never
|
|
2379
|
+
: KeyType]: ObjectType[KeyType];
|
|
2380
|
+
};
|
|
2381
|
+
|
|
2382
|
+
/**
|
|
2383
|
+
Pick only index signatures from the given object type, leaving out all explicitly defined properties.
|
|
2384
|
+
|
|
2385
|
+
This is the counterpart of `OmitIndexSignature`.
|
|
2386
|
+
|
|
2387
|
+
@example
|
|
2388
|
+
```
|
|
2389
|
+
import type {PickIndexSignature} from 'type-fest';
|
|
2390
|
+
|
|
2391
|
+
declare const symbolKey: unique symbol;
|
|
2392
|
+
|
|
2393
|
+
type Example = {
|
|
2394
|
+
// These index signatures will remain.
|
|
2395
|
+
[x: string]: unknown;
|
|
2396
|
+
[x: number]: unknown;
|
|
2397
|
+
[x: symbol]: unknown;
|
|
2398
|
+
[x: `head-${string}`]: string;
|
|
2399
|
+
[x: `${string}-tail`]: string;
|
|
2400
|
+
[x: `head-${string}-tail`]: string;
|
|
2401
|
+
[x: `${bigint}`]: string;
|
|
2402
|
+
[x: `embedded-${number}`]: string;
|
|
2403
|
+
|
|
2404
|
+
// These explicitly defined keys will be removed.
|
|
2405
|
+
['kebab-case-key']: string;
|
|
2406
|
+
[symbolKey]: string;
|
|
2407
|
+
foo: 'bar';
|
|
2408
|
+
qux?: 'baz';
|
|
2409
|
+
};
|
|
2410
|
+
|
|
2411
|
+
type ExampleIndexSignature = PickIndexSignature<Example>;
|
|
2412
|
+
// {
|
|
2413
|
+
// [x: string]: unknown;
|
|
2414
|
+
// [x: number]: unknown;
|
|
2415
|
+
// [x: symbol]: unknown;
|
|
2416
|
+
// [x: `head-${string}`]: string;
|
|
2417
|
+
// [x: `${string}-tail`]: string;
|
|
2418
|
+
// [x: `head-${string}-tail`]: string;
|
|
2419
|
+
// [x: `${bigint}`]: string;
|
|
2420
|
+
// [x: `embedded-${number}`]: string;
|
|
2421
|
+
// }
|
|
2422
|
+
```
|
|
2423
|
+
|
|
2424
|
+
@see OmitIndexSignature
|
|
2425
|
+
@category Object
|
|
2426
|
+
*/
|
|
2427
|
+
type PickIndexSignature<ObjectType> = {
|
|
2428
|
+
[KeyType in keyof ObjectType as {} extends Record<KeyType, unknown>
|
|
2429
|
+
? KeyType
|
|
2430
|
+
: never]: ObjectType[KeyType];
|
|
2431
|
+
};
|
|
2432
|
+
|
|
2433
|
+
// Merges two objects without worrying about index signatures.
|
|
2434
|
+
type SimpleMerge<Destination, Source> = {
|
|
2435
|
+
[Key in keyof Destination as Key extends keyof Source ? never : Key]: Destination[Key];
|
|
2436
|
+
} & Source;
|
|
2437
|
+
|
|
2438
|
+
/**
|
|
2439
|
+
Merge two types into a new type. Keys of the second type overrides keys of the first type.
|
|
2440
|
+
|
|
2441
|
+
@example
|
|
2442
|
+
```
|
|
2443
|
+
import type {Merge} from 'type-fest';
|
|
2444
|
+
|
|
2445
|
+
interface Foo {
|
|
2446
|
+
[x: string]: unknown;
|
|
2447
|
+
[x: number]: unknown;
|
|
2448
|
+
foo: string;
|
|
2449
|
+
bar: symbol;
|
|
2450
|
+
}
|
|
2451
|
+
|
|
2452
|
+
type Bar = {
|
|
2453
|
+
[x: number]: number;
|
|
2454
|
+
[x: symbol]: unknown;
|
|
2455
|
+
bar: Date;
|
|
2456
|
+
baz: boolean;
|
|
2457
|
+
};
|
|
2458
|
+
|
|
2459
|
+
export type FooBar = Merge<Foo, Bar>;
|
|
2460
|
+
// => {
|
|
2461
|
+
// [x: string]: unknown;
|
|
2462
|
+
// [x: number]: number;
|
|
2463
|
+
// [x: symbol]: unknown;
|
|
2464
|
+
// foo: string;
|
|
2465
|
+
// bar: Date;
|
|
2466
|
+
// baz: boolean;
|
|
2467
|
+
// }
|
|
2468
|
+
```
|
|
2469
|
+
|
|
2470
|
+
@category Object
|
|
2471
|
+
*/
|
|
2472
|
+
type Merge<Destination, Source> =
|
|
2473
|
+
Simplify<
|
|
2474
|
+
SimpleMerge<PickIndexSignature<Destination>, PickIndexSignature<Source>>
|
|
2475
|
+
& SimpleMerge<OmitIndexSignature<Destination>, OmitIndexSignature<Source>>
|
|
2476
|
+
>;
|
|
2477
|
+
|
|
2478
|
+
/**
|
|
2479
|
+
An if-else-like type that resolves depending on whether the given type is `any`.
|
|
2480
|
+
|
|
2481
|
+
@see {@link IsAny}
|
|
2482
|
+
|
|
2483
|
+
@example
|
|
2484
|
+
```
|
|
2485
|
+
import type {IfAny} from 'type-fest';
|
|
2486
|
+
|
|
2487
|
+
type ShouldBeTrue = IfAny<any>;
|
|
2488
|
+
//=> true
|
|
2489
|
+
|
|
2490
|
+
type ShouldBeBar = IfAny<'not any', 'foo', 'bar'>;
|
|
2491
|
+
//=> 'bar'
|
|
2492
|
+
```
|
|
2493
|
+
|
|
2494
|
+
@category Type Guard
|
|
2495
|
+
@category Utilities
|
|
2496
|
+
*/
|
|
2497
|
+
type IfAny<T, TypeIfAny = true, TypeIfNotAny = false> = (
|
|
2498
|
+
IsAny<T> extends true ? TypeIfAny : TypeIfNotAny
|
|
2499
|
+
);
|
|
2500
|
+
|
|
2501
|
+
/**
|
|
2502
|
+
Extract all optional keys from the given type.
|
|
2503
|
+
|
|
2504
|
+
This is useful when you want to create a new type that contains different type values for the optional keys only.
|
|
2505
|
+
|
|
2506
|
+
@example
|
|
2507
|
+
```
|
|
2508
|
+
import type {OptionalKeysOf, Except} from 'type-fest';
|
|
2509
|
+
|
|
2510
|
+
interface User {
|
|
2511
|
+
name: string;
|
|
2512
|
+
surname: string;
|
|
2513
|
+
|
|
2514
|
+
luckyNumber?: number;
|
|
2515
|
+
}
|
|
2516
|
+
|
|
2517
|
+
const REMOVE_FIELD = Symbol('remove field symbol');
|
|
2518
|
+
type UpdateOperation<Entity extends object> = Except<Partial<Entity>, OptionalKeysOf<Entity>> & {
|
|
2519
|
+
[Key in OptionalKeysOf<Entity>]?: Entity[Key] | typeof REMOVE_FIELD;
|
|
2520
|
+
};
|
|
2521
|
+
|
|
2522
|
+
const update1: UpdateOperation<User> = {
|
|
2523
|
+
name: 'Alice'
|
|
2524
|
+
};
|
|
2525
|
+
|
|
2526
|
+
const update2: UpdateOperation<User> = {
|
|
2527
|
+
name: 'Bob',
|
|
2528
|
+
luckyNumber: REMOVE_FIELD
|
|
2529
|
+
};
|
|
2530
|
+
```
|
|
2531
|
+
|
|
2532
|
+
@category Utilities
|
|
2533
|
+
*/
|
|
2534
|
+
type OptionalKeysOf<BaseType extends object> = Exclude<{
|
|
2535
|
+
[Key in keyof BaseType]: BaseType extends Record<Key, BaseType[Key]>
|
|
2536
|
+
? never
|
|
2537
|
+
: Key
|
|
2538
|
+
}[keyof BaseType], undefined>;
|
|
2539
|
+
|
|
2097
2540
|
/**
|
|
2098
2541
|
Matches any primitive, `void`, `Date`, or `RegExp` value.
|
|
2099
2542
|
*/
|
|
2100
2543
|
type BuiltIns = Primitive | void | Date | RegExp;
|
|
2101
2544
|
|
|
2545
|
+
/**
|
|
2546
|
+
Merges user specified options with default options.
|
|
2547
|
+
|
|
2548
|
+
@example
|
|
2549
|
+
```
|
|
2550
|
+
type PathsOptions = {maxRecursionDepth?: number; leavesOnly?: boolean};
|
|
2551
|
+
type DefaultPathsOptions = {maxRecursionDepth: 10; leavesOnly: false};
|
|
2552
|
+
type SpecifiedOptions = {leavesOnly: true};
|
|
2553
|
+
|
|
2554
|
+
type Result = ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, SpecifiedOptions>;
|
|
2555
|
+
//=> {maxRecursionDepth: 10; leavesOnly: true}
|
|
2556
|
+
```
|
|
2557
|
+
|
|
2558
|
+
@example
|
|
2559
|
+
```
|
|
2560
|
+
// Complains if default values are not provided for optional options
|
|
2561
|
+
|
|
2562
|
+
type PathsOptions = {maxRecursionDepth?: number; leavesOnly?: boolean};
|
|
2563
|
+
type DefaultPathsOptions = {maxRecursionDepth: 10};
|
|
2564
|
+
type SpecifiedOptions = {};
|
|
2565
|
+
|
|
2566
|
+
type Result = ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, SpecifiedOptions>;
|
|
2567
|
+
// ~~~~~~~~~~~~~~~~~~~
|
|
2568
|
+
// Property 'leavesOnly' is missing in type 'DefaultPathsOptions' but required in type '{ maxRecursionDepth: number; leavesOnly: boolean; }'.
|
|
2569
|
+
```
|
|
2570
|
+
|
|
2571
|
+
@example
|
|
2572
|
+
```
|
|
2573
|
+
// Complains if an option's default type does not conform to the expected type
|
|
2574
|
+
|
|
2575
|
+
type PathsOptions = {maxRecursionDepth?: number; leavesOnly?: boolean};
|
|
2576
|
+
type DefaultPathsOptions = {maxRecursionDepth: 10; leavesOnly: 'no'};
|
|
2577
|
+
type SpecifiedOptions = {};
|
|
2578
|
+
|
|
2579
|
+
type Result = ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, SpecifiedOptions>;
|
|
2580
|
+
// ~~~~~~~~~~~~~~~~~~~
|
|
2581
|
+
// Types of property 'leavesOnly' are incompatible. Type 'string' is not assignable to type 'boolean'.
|
|
2582
|
+
```
|
|
2583
|
+
|
|
2584
|
+
@example
|
|
2585
|
+
```
|
|
2586
|
+
// Complains if an option's specified type does not conform to the expected type
|
|
2587
|
+
|
|
2588
|
+
type PathsOptions = {maxRecursionDepth?: number; leavesOnly?: boolean};
|
|
2589
|
+
type DefaultPathsOptions = {maxRecursionDepth: 10; leavesOnly: false};
|
|
2590
|
+
type SpecifiedOptions = {leavesOnly: 'yes'};
|
|
2591
|
+
|
|
2592
|
+
type Result = ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, SpecifiedOptions>;
|
|
2593
|
+
// ~~~~~~~~~~~~~~~~
|
|
2594
|
+
// Types of property 'leavesOnly' are incompatible. Type 'string' is not assignable to type 'boolean'.
|
|
2595
|
+
```
|
|
2596
|
+
*/
|
|
2597
|
+
type ApplyDefaultOptions<
|
|
2598
|
+
Options extends object,
|
|
2599
|
+
Defaults extends Simplify<Omit<Required<Options>, RequiredKeysOf<Options>> & Partial<Record<RequiredKeysOf<Options>, never>>>,
|
|
2600
|
+
SpecifiedOptions extends Options,
|
|
2601
|
+
> =
|
|
2602
|
+
IfAny<SpecifiedOptions, Defaults,
|
|
2603
|
+
IfNever<SpecifiedOptions, Defaults,
|
|
2604
|
+
Simplify<Merge<Defaults, {
|
|
2605
|
+
[Key in keyof SpecifiedOptions
|
|
2606
|
+
as Key extends OptionalKeysOf<Options> ? undefined extends SpecifiedOptions[Key] ? never : Key : Key
|
|
2607
|
+
]: SpecifiedOptions[Key]
|
|
2608
|
+
}> & Required<Options>> // `& Required<Options>` ensures that `ApplyDefaultOptions<SomeOption, ...>` is always assignable to `Required<SomeOption>`
|
|
2609
|
+
>>;
|
|
2610
|
+
|
|
2102
2611
|
/**
|
|
2103
2612
|
@see {@link PartialDeep}
|
|
2104
2613
|
*/
|
|
@@ -2137,6 +2646,11 @@ type PartialDeepOptions = {
|
|
|
2137
2646
|
readonly allowUndefinedInNonTupleArrays?: boolean;
|
|
2138
2647
|
};
|
|
2139
2648
|
|
|
2649
|
+
type DefaultPartialDeepOptions = {
|
|
2650
|
+
recurseIntoArrays: false;
|
|
2651
|
+
allowUndefinedInNonTupleArrays: true;
|
|
2652
|
+
};
|
|
2653
|
+
|
|
2140
2654
|
/**
|
|
2141
2655
|
Create a type from another type with all keys and nested keys set to optional.
|
|
2142
2656
|
|
|
@@ -2186,7 +2700,10 @@ const partialSettings: PartialDeep<Settings, {recurseIntoArrays: true}> = {
|
|
|
2186
2700
|
@category Set
|
|
2187
2701
|
@category Map
|
|
2188
2702
|
*/
|
|
2189
|
-
type PartialDeep<T, Options extends PartialDeepOptions = {}> =
|
|
2703
|
+
type PartialDeep<T, Options extends PartialDeepOptions = {}> =
|
|
2704
|
+
_PartialDeep<T, ApplyDefaultOptions<PartialDeepOptions, DefaultPartialDeepOptions, Options>>;
|
|
2705
|
+
|
|
2706
|
+
type _PartialDeep<T, Options extends Required<PartialDeepOptions>> = T extends BuiltIns | (((...arguments_: any[]) => unknown)) | (new (...arguments_: any[]) => unknown)
|
|
2190
2707
|
? T
|
|
2191
2708
|
: T extends Map<infer KeyType, infer ValueType>
|
|
2192
2709
|
? PartialMapDeep<KeyType, ValueType, Options>
|
|
@@ -2201,8 +2718,8 @@ type PartialDeep<T, Options extends PartialDeepOptions = {}> = T extends BuiltIn
|
|
|
2201
2718
|
? Options['recurseIntoArrays'] extends true
|
|
2202
2719
|
? ItemType[] extends T // Test for arrays (non-tuples) specifically
|
|
2203
2720
|
? readonly ItemType[] extends T // Differentiate readonly and mutable arrays
|
|
2204
|
-
? ReadonlyArray<
|
|
2205
|
-
: Array<
|
|
2721
|
+
? ReadonlyArray<_PartialDeep<Options['allowUndefinedInNonTupleArrays'] extends false ? ItemType : ItemType | undefined, Options>>
|
|
2722
|
+
: Array<_PartialDeep<Options['allowUndefinedInNonTupleArrays'] extends false ? ItemType : ItemType | undefined, Options>>
|
|
2206
2723
|
: PartialObjectDeep<T, Options> // Tuples behave properly
|
|
2207
2724
|
: T // If they don't opt into array testing, just use the original type
|
|
2208
2725
|
: PartialObjectDeep<T, Options>
|
|
@@ -2211,28 +2728,28 @@ type PartialDeep<T, Options extends PartialDeepOptions = {}> = T extends BuiltIn
|
|
|
2211
2728
|
/**
|
|
2212
2729
|
Same as `PartialDeep`, but accepts only `Map`s and as inputs. Internal helper for `PartialDeep`.
|
|
2213
2730
|
*/
|
|
2214
|
-
type PartialMapDeep<KeyType, ValueType, Options extends PartialDeepOptions
|
|
2731
|
+
type PartialMapDeep<KeyType, ValueType, Options extends Required<PartialDeepOptions>> = {} & Map<_PartialDeep<KeyType, Options>, _PartialDeep<ValueType, Options>>;
|
|
2215
2732
|
|
|
2216
2733
|
/**
|
|
2217
2734
|
Same as `PartialDeep`, but accepts only `Set`s as inputs. Internal helper for `PartialDeep`.
|
|
2218
2735
|
*/
|
|
2219
|
-
type PartialSetDeep<T, Options extends PartialDeepOptions
|
|
2736
|
+
type PartialSetDeep<T, Options extends Required<PartialDeepOptions>> = {} & Set<_PartialDeep<T, Options>>;
|
|
2220
2737
|
|
|
2221
2738
|
/**
|
|
2222
2739
|
Same as `PartialDeep`, but accepts only `ReadonlyMap`s as inputs. Internal helper for `PartialDeep`.
|
|
2223
2740
|
*/
|
|
2224
|
-
type PartialReadonlyMapDeep<KeyType, ValueType, Options extends PartialDeepOptions
|
|
2741
|
+
type PartialReadonlyMapDeep<KeyType, ValueType, Options extends Required<PartialDeepOptions>> = {} & ReadonlyMap<_PartialDeep<KeyType, Options>, _PartialDeep<ValueType, Options>>;
|
|
2225
2742
|
|
|
2226
2743
|
/**
|
|
2227
2744
|
Same as `PartialDeep`, but accepts only `ReadonlySet`s as inputs. Internal helper for `PartialDeep`.
|
|
2228
2745
|
*/
|
|
2229
|
-
type PartialReadonlySetDeep<T, Options extends PartialDeepOptions
|
|
2746
|
+
type PartialReadonlySetDeep<T, Options extends Required<PartialDeepOptions>> = {} & ReadonlySet<_PartialDeep<T, Options>>;
|
|
2230
2747
|
|
|
2231
2748
|
/**
|
|
2232
2749
|
Same as `PartialDeep`, but accepts only `object`s as inputs. Internal helper for `PartialDeep`.
|
|
2233
2750
|
*/
|
|
2234
|
-
type PartialObjectDeep<ObjectType extends object, Options extends PartialDeepOptions
|
|
2235
|
-
[KeyType in keyof ObjectType]?:
|
|
2751
|
+
type PartialObjectDeep<ObjectType extends object, Options extends Required<PartialDeepOptions>> = {
|
|
2752
|
+
[KeyType in keyof ObjectType]?: _PartialDeep<ObjectType[KeyType], Options>
|
|
2236
2753
|
};
|
|
2237
2754
|
|
|
2238
2755
|
interface CustomComponentNormalized {
|
package/dist/index.d.ts
CHANGED
|
@@ -2094,11 +2094,520 @@ declare global {
|
|
|
2094
2094
|
}
|
|
2095
2095
|
}
|
|
2096
2096
|
|
|
2097
|
+
/**
|
|
2098
|
+
Extract all required keys from the given type.
|
|
2099
|
+
|
|
2100
|
+
This is useful when you want to create a new type that contains different type values for the required keys only or use the list of keys for validation purposes, etc...
|
|
2101
|
+
|
|
2102
|
+
@example
|
|
2103
|
+
```
|
|
2104
|
+
import type {RequiredKeysOf} from 'type-fest';
|
|
2105
|
+
|
|
2106
|
+
declare function createValidation<Entity extends object, Key extends RequiredKeysOf<Entity> = RequiredKeysOf<Entity>>(field: Key, validator: (value: Entity[Key]) => boolean): ValidatorFn;
|
|
2107
|
+
|
|
2108
|
+
interface User {
|
|
2109
|
+
name: string;
|
|
2110
|
+
surname: string;
|
|
2111
|
+
|
|
2112
|
+
luckyNumber?: number;
|
|
2113
|
+
}
|
|
2114
|
+
|
|
2115
|
+
const validator1 = createValidation<User>('name', value => value.length < 25);
|
|
2116
|
+
const validator2 = createValidation<User>('surname', value => value.length < 25);
|
|
2117
|
+
```
|
|
2118
|
+
|
|
2119
|
+
@category Utilities
|
|
2120
|
+
*/
|
|
2121
|
+
type RequiredKeysOf<BaseType extends object> = Exclude<{
|
|
2122
|
+
[Key in keyof BaseType]: BaseType extends Record<Key, BaseType[Key]>
|
|
2123
|
+
? Key
|
|
2124
|
+
: never
|
|
2125
|
+
}[keyof BaseType], undefined>;
|
|
2126
|
+
|
|
2127
|
+
/**
|
|
2128
|
+
Returns a boolean for whether the given type is `never`.
|
|
2129
|
+
|
|
2130
|
+
@link https://github.com/microsoft/TypeScript/issues/31751#issuecomment-498526919
|
|
2131
|
+
@link https://stackoverflow.com/a/53984913/10292952
|
|
2132
|
+
@link https://www.zhenghao.io/posts/ts-never
|
|
2133
|
+
|
|
2134
|
+
Useful in type utilities, such as checking if something does not occur.
|
|
2135
|
+
|
|
2136
|
+
@example
|
|
2137
|
+
```
|
|
2138
|
+
import type {IsNever, And} from 'type-fest';
|
|
2139
|
+
|
|
2140
|
+
// https://github.com/andnp/SimplyTyped/blob/master/src/types/strings.ts
|
|
2141
|
+
type AreStringsEqual<A extends string, B extends string> =
|
|
2142
|
+
And<
|
|
2143
|
+
IsNever<Exclude<A, B>> extends true ? true : false,
|
|
2144
|
+
IsNever<Exclude<B, A>> extends true ? true : false
|
|
2145
|
+
>;
|
|
2146
|
+
|
|
2147
|
+
type EndIfEqual<I extends string, O extends string> =
|
|
2148
|
+
AreStringsEqual<I, O> extends true
|
|
2149
|
+
? never
|
|
2150
|
+
: void;
|
|
2151
|
+
|
|
2152
|
+
function endIfEqual<I extends string, O extends string>(input: I, output: O): EndIfEqual<I, O> {
|
|
2153
|
+
if (input === output) {
|
|
2154
|
+
process.exit(0);
|
|
2155
|
+
}
|
|
2156
|
+
}
|
|
2157
|
+
|
|
2158
|
+
endIfEqual('abc', 'abc');
|
|
2159
|
+
//=> never
|
|
2160
|
+
|
|
2161
|
+
endIfEqual('abc', '123');
|
|
2162
|
+
//=> void
|
|
2163
|
+
```
|
|
2164
|
+
|
|
2165
|
+
@category Type Guard
|
|
2166
|
+
@category Utilities
|
|
2167
|
+
*/
|
|
2168
|
+
type IsNever<T> = [T] extends [never] ? true : false;
|
|
2169
|
+
|
|
2170
|
+
/**
|
|
2171
|
+
An if-else-like type that resolves depending on whether the given type is `never`.
|
|
2172
|
+
|
|
2173
|
+
@see {@link IsNever}
|
|
2174
|
+
|
|
2175
|
+
@example
|
|
2176
|
+
```
|
|
2177
|
+
import type {IfNever} from 'type-fest';
|
|
2178
|
+
|
|
2179
|
+
type ShouldBeTrue = IfNever<never>;
|
|
2180
|
+
//=> true
|
|
2181
|
+
|
|
2182
|
+
type ShouldBeBar = IfNever<'not never', 'foo', 'bar'>;
|
|
2183
|
+
//=> 'bar'
|
|
2184
|
+
```
|
|
2185
|
+
|
|
2186
|
+
@category Type Guard
|
|
2187
|
+
@category Utilities
|
|
2188
|
+
*/
|
|
2189
|
+
type IfNever<T, TypeIfNever = true, TypeIfNotNever = false> = (
|
|
2190
|
+
IsNever<T> extends true ? TypeIfNever : TypeIfNotNever
|
|
2191
|
+
);
|
|
2192
|
+
|
|
2193
|
+
// Can eventually be replaced with the built-in once this library supports
|
|
2194
|
+
// TS5.4+ only. Tracked in https://github.com/sindresorhus/type-fest/issues/848
|
|
2195
|
+
type NoInfer<T> = T extends infer U ? U : never;
|
|
2196
|
+
|
|
2197
|
+
/**
|
|
2198
|
+
Returns a boolean for whether the given type is `any`.
|
|
2199
|
+
|
|
2200
|
+
@link https://stackoverflow.com/a/49928360/1490091
|
|
2201
|
+
|
|
2202
|
+
Useful in type utilities, such as disallowing `any`s to be passed to a function.
|
|
2203
|
+
|
|
2204
|
+
@example
|
|
2205
|
+
```
|
|
2206
|
+
import type {IsAny} from 'type-fest';
|
|
2207
|
+
|
|
2208
|
+
const typedObject = {a: 1, b: 2} as const;
|
|
2209
|
+
const anyObject: any = {a: 1, b: 2};
|
|
2210
|
+
|
|
2211
|
+
function get<O extends (IsAny<O> extends true ? {} : Record<string, number>), K extends keyof O = keyof O>(obj: O, key: K) {
|
|
2212
|
+
return obj[key];
|
|
2213
|
+
}
|
|
2214
|
+
|
|
2215
|
+
const typedA = get(typedObject, 'a');
|
|
2216
|
+
//=> 1
|
|
2217
|
+
|
|
2218
|
+
const anyA = get(anyObject, 'a');
|
|
2219
|
+
//=> any
|
|
2220
|
+
```
|
|
2221
|
+
|
|
2222
|
+
@category Type Guard
|
|
2223
|
+
@category Utilities
|
|
2224
|
+
*/
|
|
2225
|
+
type IsAny<T> = 0 extends 1 & NoInfer<T> ? true : false;
|
|
2226
|
+
|
|
2227
|
+
/**
|
|
2228
|
+
Useful to flatten the type output to improve type hints shown in editors. And also to transform an interface into a type to aide with assignability.
|
|
2229
|
+
|
|
2230
|
+
@example
|
|
2231
|
+
```
|
|
2232
|
+
import type {Simplify} from 'type-fest';
|
|
2233
|
+
|
|
2234
|
+
type PositionProps = {
|
|
2235
|
+
top: number;
|
|
2236
|
+
left: number;
|
|
2237
|
+
};
|
|
2238
|
+
|
|
2239
|
+
type SizeProps = {
|
|
2240
|
+
width: number;
|
|
2241
|
+
height: number;
|
|
2242
|
+
};
|
|
2243
|
+
|
|
2244
|
+
// In your editor, hovering over `Props` will show a flattened object with all the properties.
|
|
2245
|
+
type Props = Simplify<PositionProps & SizeProps>;
|
|
2246
|
+
```
|
|
2247
|
+
|
|
2248
|
+
Sometimes it is desired to pass a value as a function argument that has a different type. At first inspection it may seem assignable, and then you discover it is not because the `value`'s type definition was defined as an interface. In the following example, `fn` requires an argument of type `Record<string, unknown>`. If the value is defined as a literal, then it is assignable. And if the `value` is defined as type using the `Simplify` utility the value is assignable. But if the `value` is defined as an interface, it is not assignable because the interface is not sealed and elsewhere a non-string property could be added to the interface.
|
|
2249
|
+
|
|
2250
|
+
If the type definition must be an interface (perhaps it was defined in a third-party npm package), then the `value` can be defined as `const value: Simplify<SomeInterface> = ...`. Then `value` will be assignable to the `fn` argument. Or the `value` can be cast as `Simplify<SomeInterface>` if you can't re-declare the `value`.
|
|
2251
|
+
|
|
2252
|
+
@example
|
|
2253
|
+
```
|
|
2254
|
+
import type {Simplify} from 'type-fest';
|
|
2255
|
+
|
|
2256
|
+
interface SomeInterface {
|
|
2257
|
+
foo: number;
|
|
2258
|
+
bar?: string;
|
|
2259
|
+
baz: number | undefined;
|
|
2260
|
+
}
|
|
2261
|
+
|
|
2262
|
+
type SomeType = {
|
|
2263
|
+
foo: number;
|
|
2264
|
+
bar?: string;
|
|
2265
|
+
baz: number | undefined;
|
|
2266
|
+
};
|
|
2267
|
+
|
|
2268
|
+
const literal = {foo: 123, bar: 'hello', baz: 456};
|
|
2269
|
+
const someType: SomeType = literal;
|
|
2270
|
+
const someInterface: SomeInterface = literal;
|
|
2271
|
+
|
|
2272
|
+
function fn(object: Record<string, unknown>): void {}
|
|
2273
|
+
|
|
2274
|
+
fn(literal); // Good: literal object type is sealed
|
|
2275
|
+
fn(someType); // Good: type is sealed
|
|
2276
|
+
fn(someInterface); // Error: Index signature for type 'string' is missing in type 'someInterface'. Because `interface` can be re-opened
|
|
2277
|
+
fn(someInterface as Simplify<SomeInterface>); // Good: transform an `interface` into a `type`
|
|
2278
|
+
```
|
|
2279
|
+
|
|
2280
|
+
@link https://github.com/microsoft/TypeScript/issues/15300
|
|
2281
|
+
@see SimplifyDeep
|
|
2282
|
+
@category Object
|
|
2283
|
+
*/
|
|
2284
|
+
type Simplify<T> = {[KeyType in keyof T]: T[KeyType]} & {};
|
|
2285
|
+
|
|
2286
|
+
/**
|
|
2287
|
+
Omit any index signatures from the given object type, leaving only explicitly defined properties.
|
|
2288
|
+
|
|
2289
|
+
This is the counterpart of `PickIndexSignature`.
|
|
2290
|
+
|
|
2291
|
+
Use-cases:
|
|
2292
|
+
- Remove overly permissive signatures from third-party types.
|
|
2293
|
+
|
|
2294
|
+
This type was taken from this [StackOverflow answer](https://stackoverflow.com/a/68261113/420747).
|
|
2295
|
+
|
|
2296
|
+
It relies on the fact that an empty object (`{}`) is assignable to an object with just an index signature, like `Record<string, unknown>`, but not to an object with explicitly defined keys, like `Record<'foo' | 'bar', unknown>`.
|
|
2297
|
+
|
|
2298
|
+
(The actual value type, `unknown`, is irrelevant and could be any type. Only the key type matters.)
|
|
2299
|
+
|
|
2300
|
+
```
|
|
2301
|
+
const indexed: Record<string, unknown> = {}; // Allowed
|
|
2302
|
+
|
|
2303
|
+
const keyed: Record<'foo', unknown> = {}; // Error
|
|
2304
|
+
// => TS2739: Type '{}' is missing the following properties from type 'Record<"foo" | "bar", unknown>': foo, bar
|
|
2305
|
+
```
|
|
2306
|
+
|
|
2307
|
+
Instead of causing a type error like the above, you can also use a [conditional type](https://www.typescriptlang.org/docs/handbook/2/conditional-types.html) to test whether a type is assignable to another:
|
|
2308
|
+
|
|
2309
|
+
```
|
|
2310
|
+
type Indexed = {} extends Record<string, unknown>
|
|
2311
|
+
? '✅ `{}` is assignable to `Record<string, unknown>`'
|
|
2312
|
+
: '❌ `{}` is NOT assignable to `Record<string, unknown>`';
|
|
2313
|
+
// => '✅ `{}` is assignable to `Record<string, unknown>`'
|
|
2314
|
+
|
|
2315
|
+
type Keyed = {} extends Record<'foo' | 'bar', unknown>
|
|
2316
|
+
? "✅ `{}` is assignable to `Record<'foo' | 'bar', unknown>`"
|
|
2317
|
+
: "❌ `{}` is NOT assignable to `Record<'foo' | 'bar', unknown>`";
|
|
2318
|
+
// => "❌ `{}` is NOT assignable to `Record<'foo' | 'bar', unknown>`"
|
|
2319
|
+
```
|
|
2320
|
+
|
|
2321
|
+
Using a [mapped type](https://www.typescriptlang.org/docs/handbook/2/mapped-types.html#further-exploration), you can then check for each `KeyType` of `ObjectType`...
|
|
2322
|
+
|
|
2323
|
+
```
|
|
2324
|
+
import type {OmitIndexSignature} from 'type-fest';
|
|
2325
|
+
|
|
2326
|
+
type OmitIndexSignature<ObjectType> = {
|
|
2327
|
+
[KeyType in keyof ObjectType // Map each key of `ObjectType`...
|
|
2328
|
+
]: ObjectType[KeyType]; // ...to its original value, i.e. `OmitIndexSignature<Foo> == Foo`.
|
|
2329
|
+
};
|
|
2330
|
+
```
|
|
2331
|
+
|
|
2332
|
+
...whether an empty object (`{}`) would be assignable to an object with that `KeyType` (`Record<KeyType, unknown>`)...
|
|
2333
|
+
|
|
2334
|
+
```
|
|
2335
|
+
import type {OmitIndexSignature} from 'type-fest';
|
|
2336
|
+
|
|
2337
|
+
type OmitIndexSignature<ObjectType> = {
|
|
2338
|
+
[KeyType in keyof ObjectType
|
|
2339
|
+
// Is `{}` assignable to `Record<KeyType, unknown>`?
|
|
2340
|
+
as {} extends Record<KeyType, unknown>
|
|
2341
|
+
? ... // ✅ `{}` is assignable to `Record<KeyType, unknown>`
|
|
2342
|
+
: ... // ❌ `{}` is NOT assignable to `Record<KeyType, unknown>`
|
|
2343
|
+
]: ObjectType[KeyType];
|
|
2344
|
+
};
|
|
2345
|
+
```
|
|
2346
|
+
|
|
2347
|
+
If `{}` is assignable, it means that `KeyType` is an index signature and we want to remove it. If it is not assignable, `KeyType` is a "real" key and we want to keep it.
|
|
2348
|
+
|
|
2349
|
+
@example
|
|
2350
|
+
```
|
|
2351
|
+
import type {OmitIndexSignature} from 'type-fest';
|
|
2352
|
+
|
|
2353
|
+
interface Example {
|
|
2354
|
+
// These index signatures will be removed.
|
|
2355
|
+
[x: string]: any
|
|
2356
|
+
[x: number]: any
|
|
2357
|
+
[x: symbol]: any
|
|
2358
|
+
[x: `head-${string}`]: string
|
|
2359
|
+
[x: `${string}-tail`]: string
|
|
2360
|
+
[x: `head-${string}-tail`]: string
|
|
2361
|
+
[x: `${bigint}`]: string
|
|
2362
|
+
[x: `embedded-${number}`]: string
|
|
2363
|
+
|
|
2364
|
+
// These explicitly defined keys will remain.
|
|
2365
|
+
foo: 'bar';
|
|
2366
|
+
qux?: 'baz';
|
|
2367
|
+
}
|
|
2368
|
+
|
|
2369
|
+
type ExampleWithoutIndexSignatures = OmitIndexSignature<Example>;
|
|
2370
|
+
// => { foo: 'bar'; qux?: 'baz' | undefined; }
|
|
2371
|
+
```
|
|
2372
|
+
|
|
2373
|
+
@see PickIndexSignature
|
|
2374
|
+
@category Object
|
|
2375
|
+
*/
|
|
2376
|
+
type OmitIndexSignature<ObjectType> = {
|
|
2377
|
+
[KeyType in keyof ObjectType as {} extends Record<KeyType, unknown>
|
|
2378
|
+
? never
|
|
2379
|
+
: KeyType]: ObjectType[KeyType];
|
|
2380
|
+
};
|
|
2381
|
+
|
|
2382
|
+
/**
|
|
2383
|
+
Pick only index signatures from the given object type, leaving out all explicitly defined properties.
|
|
2384
|
+
|
|
2385
|
+
This is the counterpart of `OmitIndexSignature`.
|
|
2386
|
+
|
|
2387
|
+
@example
|
|
2388
|
+
```
|
|
2389
|
+
import type {PickIndexSignature} from 'type-fest';
|
|
2390
|
+
|
|
2391
|
+
declare const symbolKey: unique symbol;
|
|
2392
|
+
|
|
2393
|
+
type Example = {
|
|
2394
|
+
// These index signatures will remain.
|
|
2395
|
+
[x: string]: unknown;
|
|
2396
|
+
[x: number]: unknown;
|
|
2397
|
+
[x: symbol]: unknown;
|
|
2398
|
+
[x: `head-${string}`]: string;
|
|
2399
|
+
[x: `${string}-tail`]: string;
|
|
2400
|
+
[x: `head-${string}-tail`]: string;
|
|
2401
|
+
[x: `${bigint}`]: string;
|
|
2402
|
+
[x: `embedded-${number}`]: string;
|
|
2403
|
+
|
|
2404
|
+
// These explicitly defined keys will be removed.
|
|
2405
|
+
['kebab-case-key']: string;
|
|
2406
|
+
[symbolKey]: string;
|
|
2407
|
+
foo: 'bar';
|
|
2408
|
+
qux?: 'baz';
|
|
2409
|
+
};
|
|
2410
|
+
|
|
2411
|
+
type ExampleIndexSignature = PickIndexSignature<Example>;
|
|
2412
|
+
// {
|
|
2413
|
+
// [x: string]: unknown;
|
|
2414
|
+
// [x: number]: unknown;
|
|
2415
|
+
// [x: symbol]: unknown;
|
|
2416
|
+
// [x: `head-${string}`]: string;
|
|
2417
|
+
// [x: `${string}-tail`]: string;
|
|
2418
|
+
// [x: `head-${string}-tail`]: string;
|
|
2419
|
+
// [x: `${bigint}`]: string;
|
|
2420
|
+
// [x: `embedded-${number}`]: string;
|
|
2421
|
+
// }
|
|
2422
|
+
```
|
|
2423
|
+
|
|
2424
|
+
@see OmitIndexSignature
|
|
2425
|
+
@category Object
|
|
2426
|
+
*/
|
|
2427
|
+
type PickIndexSignature<ObjectType> = {
|
|
2428
|
+
[KeyType in keyof ObjectType as {} extends Record<KeyType, unknown>
|
|
2429
|
+
? KeyType
|
|
2430
|
+
: never]: ObjectType[KeyType];
|
|
2431
|
+
};
|
|
2432
|
+
|
|
2433
|
+
// Merges two objects without worrying about index signatures.
|
|
2434
|
+
type SimpleMerge<Destination, Source> = {
|
|
2435
|
+
[Key in keyof Destination as Key extends keyof Source ? never : Key]: Destination[Key];
|
|
2436
|
+
} & Source;
|
|
2437
|
+
|
|
2438
|
+
/**
|
|
2439
|
+
Merge two types into a new type. Keys of the second type overrides keys of the first type.
|
|
2440
|
+
|
|
2441
|
+
@example
|
|
2442
|
+
```
|
|
2443
|
+
import type {Merge} from 'type-fest';
|
|
2444
|
+
|
|
2445
|
+
interface Foo {
|
|
2446
|
+
[x: string]: unknown;
|
|
2447
|
+
[x: number]: unknown;
|
|
2448
|
+
foo: string;
|
|
2449
|
+
bar: symbol;
|
|
2450
|
+
}
|
|
2451
|
+
|
|
2452
|
+
type Bar = {
|
|
2453
|
+
[x: number]: number;
|
|
2454
|
+
[x: symbol]: unknown;
|
|
2455
|
+
bar: Date;
|
|
2456
|
+
baz: boolean;
|
|
2457
|
+
};
|
|
2458
|
+
|
|
2459
|
+
export type FooBar = Merge<Foo, Bar>;
|
|
2460
|
+
// => {
|
|
2461
|
+
// [x: string]: unknown;
|
|
2462
|
+
// [x: number]: number;
|
|
2463
|
+
// [x: symbol]: unknown;
|
|
2464
|
+
// foo: string;
|
|
2465
|
+
// bar: Date;
|
|
2466
|
+
// baz: boolean;
|
|
2467
|
+
// }
|
|
2468
|
+
```
|
|
2469
|
+
|
|
2470
|
+
@category Object
|
|
2471
|
+
*/
|
|
2472
|
+
type Merge<Destination, Source> =
|
|
2473
|
+
Simplify<
|
|
2474
|
+
SimpleMerge<PickIndexSignature<Destination>, PickIndexSignature<Source>>
|
|
2475
|
+
& SimpleMerge<OmitIndexSignature<Destination>, OmitIndexSignature<Source>>
|
|
2476
|
+
>;
|
|
2477
|
+
|
|
2478
|
+
/**
|
|
2479
|
+
An if-else-like type that resolves depending on whether the given type is `any`.
|
|
2480
|
+
|
|
2481
|
+
@see {@link IsAny}
|
|
2482
|
+
|
|
2483
|
+
@example
|
|
2484
|
+
```
|
|
2485
|
+
import type {IfAny} from 'type-fest';
|
|
2486
|
+
|
|
2487
|
+
type ShouldBeTrue = IfAny<any>;
|
|
2488
|
+
//=> true
|
|
2489
|
+
|
|
2490
|
+
type ShouldBeBar = IfAny<'not any', 'foo', 'bar'>;
|
|
2491
|
+
//=> 'bar'
|
|
2492
|
+
```
|
|
2493
|
+
|
|
2494
|
+
@category Type Guard
|
|
2495
|
+
@category Utilities
|
|
2496
|
+
*/
|
|
2497
|
+
type IfAny<T, TypeIfAny = true, TypeIfNotAny = false> = (
|
|
2498
|
+
IsAny<T> extends true ? TypeIfAny : TypeIfNotAny
|
|
2499
|
+
);
|
|
2500
|
+
|
|
2501
|
+
/**
|
|
2502
|
+
Extract all optional keys from the given type.
|
|
2503
|
+
|
|
2504
|
+
This is useful when you want to create a new type that contains different type values for the optional keys only.
|
|
2505
|
+
|
|
2506
|
+
@example
|
|
2507
|
+
```
|
|
2508
|
+
import type {OptionalKeysOf, Except} from 'type-fest';
|
|
2509
|
+
|
|
2510
|
+
interface User {
|
|
2511
|
+
name: string;
|
|
2512
|
+
surname: string;
|
|
2513
|
+
|
|
2514
|
+
luckyNumber?: number;
|
|
2515
|
+
}
|
|
2516
|
+
|
|
2517
|
+
const REMOVE_FIELD = Symbol('remove field symbol');
|
|
2518
|
+
type UpdateOperation<Entity extends object> = Except<Partial<Entity>, OptionalKeysOf<Entity>> & {
|
|
2519
|
+
[Key in OptionalKeysOf<Entity>]?: Entity[Key] | typeof REMOVE_FIELD;
|
|
2520
|
+
};
|
|
2521
|
+
|
|
2522
|
+
const update1: UpdateOperation<User> = {
|
|
2523
|
+
name: 'Alice'
|
|
2524
|
+
};
|
|
2525
|
+
|
|
2526
|
+
const update2: UpdateOperation<User> = {
|
|
2527
|
+
name: 'Bob',
|
|
2528
|
+
luckyNumber: REMOVE_FIELD
|
|
2529
|
+
};
|
|
2530
|
+
```
|
|
2531
|
+
|
|
2532
|
+
@category Utilities
|
|
2533
|
+
*/
|
|
2534
|
+
type OptionalKeysOf<BaseType extends object> = Exclude<{
|
|
2535
|
+
[Key in keyof BaseType]: BaseType extends Record<Key, BaseType[Key]>
|
|
2536
|
+
? never
|
|
2537
|
+
: Key
|
|
2538
|
+
}[keyof BaseType], undefined>;
|
|
2539
|
+
|
|
2097
2540
|
/**
|
|
2098
2541
|
Matches any primitive, `void`, `Date`, or `RegExp` value.
|
|
2099
2542
|
*/
|
|
2100
2543
|
type BuiltIns = Primitive | void | Date | RegExp;
|
|
2101
2544
|
|
|
2545
|
+
/**
|
|
2546
|
+
Merges user specified options with default options.
|
|
2547
|
+
|
|
2548
|
+
@example
|
|
2549
|
+
```
|
|
2550
|
+
type PathsOptions = {maxRecursionDepth?: number; leavesOnly?: boolean};
|
|
2551
|
+
type DefaultPathsOptions = {maxRecursionDepth: 10; leavesOnly: false};
|
|
2552
|
+
type SpecifiedOptions = {leavesOnly: true};
|
|
2553
|
+
|
|
2554
|
+
type Result = ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, SpecifiedOptions>;
|
|
2555
|
+
//=> {maxRecursionDepth: 10; leavesOnly: true}
|
|
2556
|
+
```
|
|
2557
|
+
|
|
2558
|
+
@example
|
|
2559
|
+
```
|
|
2560
|
+
// Complains if default values are not provided for optional options
|
|
2561
|
+
|
|
2562
|
+
type PathsOptions = {maxRecursionDepth?: number; leavesOnly?: boolean};
|
|
2563
|
+
type DefaultPathsOptions = {maxRecursionDepth: 10};
|
|
2564
|
+
type SpecifiedOptions = {};
|
|
2565
|
+
|
|
2566
|
+
type Result = ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, SpecifiedOptions>;
|
|
2567
|
+
// ~~~~~~~~~~~~~~~~~~~
|
|
2568
|
+
// Property 'leavesOnly' is missing in type 'DefaultPathsOptions' but required in type '{ maxRecursionDepth: number; leavesOnly: boolean; }'.
|
|
2569
|
+
```
|
|
2570
|
+
|
|
2571
|
+
@example
|
|
2572
|
+
```
|
|
2573
|
+
// Complains if an option's default type does not conform to the expected type
|
|
2574
|
+
|
|
2575
|
+
type PathsOptions = {maxRecursionDepth?: number; leavesOnly?: boolean};
|
|
2576
|
+
type DefaultPathsOptions = {maxRecursionDepth: 10; leavesOnly: 'no'};
|
|
2577
|
+
type SpecifiedOptions = {};
|
|
2578
|
+
|
|
2579
|
+
type Result = ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, SpecifiedOptions>;
|
|
2580
|
+
// ~~~~~~~~~~~~~~~~~~~
|
|
2581
|
+
// Types of property 'leavesOnly' are incompatible. Type 'string' is not assignable to type 'boolean'.
|
|
2582
|
+
```
|
|
2583
|
+
|
|
2584
|
+
@example
|
|
2585
|
+
```
|
|
2586
|
+
// Complains if an option's specified type does not conform to the expected type
|
|
2587
|
+
|
|
2588
|
+
type PathsOptions = {maxRecursionDepth?: number; leavesOnly?: boolean};
|
|
2589
|
+
type DefaultPathsOptions = {maxRecursionDepth: 10; leavesOnly: false};
|
|
2590
|
+
type SpecifiedOptions = {leavesOnly: 'yes'};
|
|
2591
|
+
|
|
2592
|
+
type Result = ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, SpecifiedOptions>;
|
|
2593
|
+
// ~~~~~~~~~~~~~~~~
|
|
2594
|
+
// Types of property 'leavesOnly' are incompatible. Type 'string' is not assignable to type 'boolean'.
|
|
2595
|
+
```
|
|
2596
|
+
*/
|
|
2597
|
+
type ApplyDefaultOptions<
|
|
2598
|
+
Options extends object,
|
|
2599
|
+
Defaults extends Simplify<Omit<Required<Options>, RequiredKeysOf<Options>> & Partial<Record<RequiredKeysOf<Options>, never>>>,
|
|
2600
|
+
SpecifiedOptions extends Options,
|
|
2601
|
+
> =
|
|
2602
|
+
IfAny<SpecifiedOptions, Defaults,
|
|
2603
|
+
IfNever<SpecifiedOptions, Defaults,
|
|
2604
|
+
Simplify<Merge<Defaults, {
|
|
2605
|
+
[Key in keyof SpecifiedOptions
|
|
2606
|
+
as Key extends OptionalKeysOf<Options> ? undefined extends SpecifiedOptions[Key] ? never : Key : Key
|
|
2607
|
+
]: SpecifiedOptions[Key]
|
|
2608
|
+
}> & Required<Options>> // `& Required<Options>` ensures that `ApplyDefaultOptions<SomeOption, ...>` is always assignable to `Required<SomeOption>`
|
|
2609
|
+
>>;
|
|
2610
|
+
|
|
2102
2611
|
/**
|
|
2103
2612
|
@see {@link PartialDeep}
|
|
2104
2613
|
*/
|
|
@@ -2137,6 +2646,11 @@ type PartialDeepOptions = {
|
|
|
2137
2646
|
readonly allowUndefinedInNonTupleArrays?: boolean;
|
|
2138
2647
|
};
|
|
2139
2648
|
|
|
2649
|
+
type DefaultPartialDeepOptions = {
|
|
2650
|
+
recurseIntoArrays: false;
|
|
2651
|
+
allowUndefinedInNonTupleArrays: true;
|
|
2652
|
+
};
|
|
2653
|
+
|
|
2140
2654
|
/**
|
|
2141
2655
|
Create a type from another type with all keys and nested keys set to optional.
|
|
2142
2656
|
|
|
@@ -2186,7 +2700,10 @@ const partialSettings: PartialDeep<Settings, {recurseIntoArrays: true}> = {
|
|
|
2186
2700
|
@category Set
|
|
2187
2701
|
@category Map
|
|
2188
2702
|
*/
|
|
2189
|
-
type PartialDeep<T, Options extends PartialDeepOptions = {}> =
|
|
2703
|
+
type PartialDeep<T, Options extends PartialDeepOptions = {}> =
|
|
2704
|
+
_PartialDeep<T, ApplyDefaultOptions<PartialDeepOptions, DefaultPartialDeepOptions, Options>>;
|
|
2705
|
+
|
|
2706
|
+
type _PartialDeep<T, Options extends Required<PartialDeepOptions>> = T extends BuiltIns | (((...arguments_: any[]) => unknown)) | (new (...arguments_: any[]) => unknown)
|
|
2190
2707
|
? T
|
|
2191
2708
|
: T extends Map<infer KeyType, infer ValueType>
|
|
2192
2709
|
? PartialMapDeep<KeyType, ValueType, Options>
|
|
@@ -2201,8 +2718,8 @@ type PartialDeep<T, Options extends PartialDeepOptions = {}> = T extends BuiltIn
|
|
|
2201
2718
|
? Options['recurseIntoArrays'] extends true
|
|
2202
2719
|
? ItemType[] extends T // Test for arrays (non-tuples) specifically
|
|
2203
2720
|
? readonly ItemType[] extends T // Differentiate readonly and mutable arrays
|
|
2204
|
-
? ReadonlyArray<
|
|
2205
|
-
: Array<
|
|
2721
|
+
? ReadonlyArray<_PartialDeep<Options['allowUndefinedInNonTupleArrays'] extends false ? ItemType : ItemType | undefined, Options>>
|
|
2722
|
+
: Array<_PartialDeep<Options['allowUndefinedInNonTupleArrays'] extends false ? ItemType : ItemType | undefined, Options>>
|
|
2206
2723
|
: PartialObjectDeep<T, Options> // Tuples behave properly
|
|
2207
2724
|
: T // If they don't opt into array testing, just use the original type
|
|
2208
2725
|
: PartialObjectDeep<T, Options>
|
|
@@ -2211,28 +2728,28 @@ type PartialDeep<T, Options extends PartialDeepOptions = {}> = T extends BuiltIn
|
|
|
2211
2728
|
/**
|
|
2212
2729
|
Same as `PartialDeep`, but accepts only `Map`s and as inputs. Internal helper for `PartialDeep`.
|
|
2213
2730
|
*/
|
|
2214
|
-
type PartialMapDeep<KeyType, ValueType, Options extends PartialDeepOptions
|
|
2731
|
+
type PartialMapDeep<KeyType, ValueType, Options extends Required<PartialDeepOptions>> = {} & Map<_PartialDeep<KeyType, Options>, _PartialDeep<ValueType, Options>>;
|
|
2215
2732
|
|
|
2216
2733
|
/**
|
|
2217
2734
|
Same as `PartialDeep`, but accepts only `Set`s as inputs. Internal helper for `PartialDeep`.
|
|
2218
2735
|
*/
|
|
2219
|
-
type PartialSetDeep<T, Options extends PartialDeepOptions
|
|
2736
|
+
type PartialSetDeep<T, Options extends Required<PartialDeepOptions>> = {} & Set<_PartialDeep<T, Options>>;
|
|
2220
2737
|
|
|
2221
2738
|
/**
|
|
2222
2739
|
Same as `PartialDeep`, but accepts only `ReadonlyMap`s as inputs. Internal helper for `PartialDeep`.
|
|
2223
2740
|
*/
|
|
2224
|
-
type PartialReadonlyMapDeep<KeyType, ValueType, Options extends PartialDeepOptions
|
|
2741
|
+
type PartialReadonlyMapDeep<KeyType, ValueType, Options extends Required<PartialDeepOptions>> = {} & ReadonlyMap<_PartialDeep<KeyType, Options>, _PartialDeep<ValueType, Options>>;
|
|
2225
2742
|
|
|
2226
2743
|
/**
|
|
2227
2744
|
Same as `PartialDeep`, but accepts only `ReadonlySet`s as inputs. Internal helper for `PartialDeep`.
|
|
2228
2745
|
*/
|
|
2229
|
-
type PartialReadonlySetDeep<T, Options extends PartialDeepOptions
|
|
2746
|
+
type PartialReadonlySetDeep<T, Options extends Required<PartialDeepOptions>> = {} & ReadonlySet<_PartialDeep<T, Options>>;
|
|
2230
2747
|
|
|
2231
2748
|
/**
|
|
2232
2749
|
Same as `PartialDeep`, but accepts only `object`s as inputs. Internal helper for `PartialDeep`.
|
|
2233
2750
|
*/
|
|
2234
|
-
type PartialObjectDeep<ObjectType extends object, Options extends PartialDeepOptions
|
|
2235
|
-
[KeyType in keyof ObjectType]?:
|
|
2751
|
+
type PartialObjectDeep<ObjectType extends object, Options extends Required<PartialDeepOptions>> = {
|
|
2752
|
+
[KeyType in keyof ObjectType]?: _PartialDeep<ObjectType[KeyType], Options>
|
|
2236
2753
|
};
|
|
2237
2754
|
|
|
2238
2755
|
interface CustomComponentNormalized {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eslint-react/shared",
|
|
3
|
-
"version": "1.38.0-next.
|
|
3
|
+
"version": "1.38.0-next.7",
|
|
4
4
|
"description": "ESLint React's Shared constants and functions.",
|
|
5
5
|
"homepage": "https://github.com/Rel1cx/eslint-react",
|
|
6
6
|
"bugs": {
|
|
@@ -38,16 +38,16 @@
|
|
|
38
38
|
"@typescript-eslint/utils": "^8.27.0",
|
|
39
39
|
"picomatch": "^4.0.2",
|
|
40
40
|
"ts-pattern": "^5.6.2",
|
|
41
|
-
"@eslint-react/kit": "1.38.0-next.
|
|
42
|
-
"@eslint-react/eff": "1.38.0-next.
|
|
41
|
+
"@eslint-react/kit": "1.38.0-next.7",
|
|
42
|
+
"@eslint-react/eff": "1.38.0-next.7"
|
|
43
43
|
},
|
|
44
44
|
"devDependencies": {
|
|
45
|
-
"@tsconfig/node22": "^22.0.
|
|
45
|
+
"@tsconfig/node22": "^22.0.1",
|
|
46
46
|
"@types/picomatch": "^3.0.2",
|
|
47
47
|
"fast-equals": "^5.2.2",
|
|
48
48
|
"micro-memoize": "^4.1.3",
|
|
49
49
|
"tsup": "^8.4.0",
|
|
50
|
-
"type-fest": "^4.
|
|
50
|
+
"type-fest": "^4.38.0",
|
|
51
51
|
"valibot": "^1.0.0",
|
|
52
52
|
"@local/configs": "0.0.0"
|
|
53
53
|
},
|