Skip to content

Commit f0af103

Browse files
authored
Fix syntax for specifying types in certain JSDoc codeblocks (#1362)
1 parent cfea505 commit f0af103

File tree

9 files changed

+18
-60
lines changed

9 files changed

+18
-60
lines changed

source/array-splice.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ Split the given array `T` by the given `SplitIndex`.
5050
@example
5151
```
5252
type A = SplitArrayByIndex<[1, 2, 3, 4], 2>;
53-
// type A = [[1, 2], [3, 4]];
53+
//=> [[1, 2], [3, 4]];
5454
5555
type B = SplitArrayByIndex<[1, 2, 3, 4], 0>;
56-
// type B = [[], [1, 2, 3, 4]];
56+
//=> [[], [1, 2, 3, 4]];
5757
```
5858
*/
5959
type SplitArrayByIndex<T extends UnknownArray, SplitIndex extends number> =

source/pick-deep.d.ts

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type {Paths} from './paths.d.ts';
55
import type {Simplify} from './simplify.d.ts';
66
import type {UnionToIntersection} from './union-to-intersection.d.ts';
77
import type {UnknownArray} from './unknown-array.d.ts';
8+
import type {SimplifyDeep} from './simplify-deep.d.ts';
89

910
/**
1011
Pick properties from a deeply-nested object.
@@ -36,24 +37,15 @@ type Configuration = {
3637
};
3738
3839
type NameConfig = PickDeep<Configuration, 'userConfig.name'>;
39-
// type NameConfig = {
40-
// userConfig: {
41-
// name: string;
42-
// }
43-
// };
40+
//=> {userConfig: {name: string}}
4441
4542
// Supports optional properties
4643
type User = PickDeep<PartialDeep<Configuration>, 'userConfig.name' | 'userConfig.age'>;
47-
// type User = {
48-
// userConfig?: {
49-
// name?: string;
50-
// age?: number;
51-
// };
52-
// };
44+
//=> {userConfig?: {name?: string; age?: number}}
5345
5446
// Supports array
5547
type AddressConfig = PickDeep<Configuration, 'userConfig.address.0'>;
56-
// type AddressConfig = {
48+
//=> {
5749
// userConfig: {
5850
// address: [{
5951
// city1: string;
@@ -64,14 +56,7 @@ type AddressConfig = PickDeep<Configuration, 'userConfig.address.0'>;
6456
6557
// Supports recurse into array
6658
type Street = PickDeep<Configuration, 'userConfig.address.1.street2'>;
67-
// type Street = {
68-
// userConfig: {
69-
// address: [
70-
// unknown,
71-
// {street2: string}
72-
// ];
73-
// };
74-
// }
59+
//=> {userConfig: {address: [unknown, {street2: string}]}}
7560
```
7661
7762
@category Object
@@ -86,7 +71,7 @@ export type PickDeep<T, PathUnion extends Paths<T>> =
8671
}[PathUnion]
8772
>
8873
: T extends object
89-
? Simplify<UnionToIntersection<{
74+
? SimplifyDeep<UnionToIntersection<{
9075
[P in PathUnion]: InternalPickDeep<T, P>;
9176
}[PathUnion]>>
9277
: never;

source/set-non-nullable.d.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,19 @@ type Foo = {
1515
c?: boolean | null;
1616
};
1717
18+
// Note: In the following example, `c` can no longer be `null`, but it's still optional.
1819
type SomeNonNullable = SetNonNullable<Foo, 'b' | 'c'>;
19-
// type SomeNonNullable = {
20-
// a: number | null;
21-
// b: string; // Can no longer be undefined.
22-
// c?: boolean; // Can no longer be null, but is still optional.
23-
// }
20+
//=> {a: null | number; b: string; c?: boolean}
2421
2522
type AllNonNullable = SetNonNullable<Foo>;
26-
// type AllNonNullable = {
27-
// a: number; // Can no longer be null.
28-
// b: string; // Can no longer be undefined.
29-
// c?: boolean; // Can no longer be null, but is still optional.
30-
// }
23+
//=> {a: number; b: string; c?: boolean}
3124
```
3225
3326
@category Object
3427
*/
3528
export type SetNonNullable<BaseType, Keys extends keyof BaseType = keyof BaseType> = {
3629
[Key in keyof BaseType]: Key extends Keys
37-
? NonNullable<BaseType[Key]>
30+
? BaseType[Key] & {} // `& {}` is used instead of `NonNullable<BaseType[Key]>` because `NonNullable` doesn't get simplified.
3831
: BaseType[Key];
3932
};
4033

source/set-optional.d.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,7 @@ type Foo = {
1818
};
1919
2020
type SomeOptional = SetOptional<Foo, 'b' | 'c'>;
21-
// type SomeOptional = {
22-
// a: number;
23-
// b?: string; // Was already optional and still is.
24-
// c?: boolean; // Is now optional.
25-
// }
21+
//=> {a: number; b?: string; c?: boolean}
2622
```
2723
2824
@category Object

source/set-readonly.d.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,7 @@ type Foo = {
1818
};
1919
2020
type SomeReadonly = SetReadonly<Foo, 'b' | 'c'>;
21-
// type SomeReadonly = {
22-
// a: number;
23-
// readonly b: string; // Was already readonly and still is.
24-
// readonly c: boolean; // Is now readonly.
25-
// }
21+
//=> {a: number; readonly b: string; readonly c: boolean}
2622
```
2723
2824
@category Object

source/set-required.d.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,7 @@ type Foo = {
2121
};
2222
2323
type SomeRequired = SetRequired<Foo, 'b' | 'c'>;
24-
// type SomeRequired = {
25-
// a?: number;
26-
// b: string; // Was already required and still is.
27-
// c: boolean; // Is now required.
28-
// }
24+
//=> {a?: number; b: string; c: boolean}
2925
3026
// Set specific indices in an array to be required.
3127
type ArrayExample = SetRequired<[number?, number?, number?], 0 | 1>;

source/spread.d.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,7 @@ const bar = {c: false};
4141
const fooBar = {...foo, ...bar};
4242
4343
type FooBar = Spread<Foo, Bar>;
44-
// type FooBar = {
45-
// a: number;
46-
// b?: string | number | undefined;
47-
// c: boolean;
48-
// }
44+
//=> {a: number; b?: string | number; c: boolean}
4945
5046
declare function baz(argument: FooBar): void;
5147

source/writable.d.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,7 @@ writableFoo.b[0] = 'new value'; // Will still fail as the value of property "b"
3737
writableFoo.b = ['something']; // Will work as the "b" property itself is no longer readonly.
3838
3939
type SomeWritable = Writable<Foo, 'b' | 'c'>;
40-
// type SomeWritable = {
41-
// readonly a: number;
42-
// b: readonly string[]; // It's now writable. The type of the property remains unaffected.
43-
// c: boolean; // It's now writable.
44-
// }
40+
//=> {readonly a: number; b: readonly string[]; c: boolean}
4541
4642
// Also supports array
4743
const readonlyArray: readonly number[] = [1, 2, 3];

test-d/pick-deep.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ declare const genericTest: PickDeep<GenericType<number>, 'genericKey'>;
7171
expectType<{genericKey: number}>(genericTest);
7272

7373
declare const union: PickDeep<Testing, 'object.number' | 'object.string'>;
74-
expectType<{object: {number: number} & {string: string}}>(union);
74+
expectType<{object: {number: number; string: string}}>(union);
7575

7676
declare const optional: PickDeep<Testing, 'optionalObject.optionalString'>;
7777
expectType<{optionalObject?: {optionalString?: string}}>(optional);

0 commit comments

Comments
 (0)