Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(tools): add docs for type utils and cover those utils by tests #18584

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions tools/types.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { KnownKeys, RemoveRecordIndexSignature } from './types';

describe(`types`, () => {
describe(`utils`, () => {
describe(`#KnownKeys`, () => {
it(`should get keys from record except index signatures`, () => {
type RecordWithIndexType = { one: number; two: string; [k: string]: unknown };

// $ExpectType 'one' | 'two'
type Test = KnownKeys<RecordWithIndexType>;

// @ts-expect-error - testing types
const assertion: Test = 'random';
expect(assertion).toBeDefined();
});
});

describe(`#RemoveRecordIndexSignature`, () => {
it(`it should get record shape without index signatures`, () => {
type RecordWithIndexType = { one: number; two: string; [k: string]: unknown };

// $ExpectType { one: number; two: string; }
type Test = RemoveRecordIndexSignature<RecordWithIndexType>;

// @ts-expect-error - testing types
const assertion: Test = { one: 1, two: '2', fooBar: true };
expect(assertion).toBeDefined();
});
});
});
});
46 changes: 38 additions & 8 deletions tools/types.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
type RemoveIndex<T extends Record<string, unknown>> = Pick<T, KnownKeys<T>>;
type KnownKeys<T> = {
[K in keyof T]: string extends K ? never : number extends K ? never : K;
} extends { [_ in keyof T]: infer U }
? U
: never;

type TsOriginalCompilerOptions = import('typescript').CompilerOptions;
interface CompilerOptions
extends Omit<RemoveIndex<TsOriginalCompilerOptions>, 'module' | 'target' | 'jsx' | 'moduleResolution'> {
extends Omit<
RemoveRecordIndexSignature<TsOriginalCompilerOptions>,
'module' | 'target' | 'jsx' | 'moduleResolution'
> {
module?: keyof typeof import('typescript').ModuleKind;
target?: keyof typeof import('typescript').ScriptTarget;
jsx?: 'none' | 'preserve' | 'react' | 'react-native' | 'react-jsx' | 'react-jsxdev';
Expand All @@ -32,3 +28,37 @@ export interface PackageJson {
devDependencies?: Record<string, string>;
peerDependencies?: Record<string, string>;
}

// ===============
// Type Utilities
// ===============

/**
* Removes index signature `[key:string]: any` from Dictionary/Record
*
* @example
*
* ```ts
* type MapWithIndexType = { one: number; two: string; [k: string]: any; }
* // $ExpectType { one: number; two: string; }
* type Test = RemoveRecordIndexSignature<MapWithIndexType>
* ```
*/
export type RemoveRecordIndexSignature<T extends Record<string, unknown>> = Pick<T, KnownKeys<T>>;

/**
* Get proper known keys from object which contains index type `[key:string]: any`
*
* @example
*
* ```ts
* type MapWithIndexType = { one: number; two: string; [k: string]: any; }
* // $ExpectType 'one' | 'two'
* type Test = KnownKeys<MapWithIndexType>
* ```
*/
export type KnownKeys<T> = {
[K in keyof T]: string extends K ? never : number extends K ? never : K;
} extends { [_ in keyof T]: infer U }
? U
: never;