Skip to main content
Module

x/scaffold/src/deps/types.ts>RemoveIndexSignature

scaffold your next project with style and 💗
Latest
type alias RemoveIndexSignature
import { type RemoveIndexSignature } from "https://deno.land/x/scaffold@0.3.0/src/deps/types.ts";

Remove any index signatures from the given object type, so that only explicitly defined properties remain.

Use-cases:

  • Remove overly permissive signatures from third-party types.

This type was taken from this StackOverflow answer.

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>.

(The actual value type, unknown, is irrelevant and could be any type. Only the key type matters.)

const indexed: Record<string, unknown> = {}; // Allowed

const keyed: Record<'foo', unknown> = {}; // Error
// => TS2739: Type '{}' is missing the following properties from type 'Record<"foo" | "bar", unknown>': foo, bar

Instead of causing a type error like the above, you can also use a conditional type to test whether a type is assignable to another:

type Indexed = {} extends Record<string, unknown>
	? '✅ `{}` is assignable to `Record<string, unknown>`'
	: '❌ `{}` is NOT assignable to `Record<string, unknown>`';
// => '✅ `{}` is assignable to `Record<string, unknown>`'

type Keyed = {} extends Record<'foo' | 'bar', unknown>
	? "✅ `{}` is assignable to `Record<'foo' | 'bar', unknown>`"
	: "❌ `{}` is NOT assignable to `Record<'foo' | 'bar', unknown>`";
// => "❌ `{}` is NOT assignable to `Record<'foo' | 'bar', unknown>`"

Using a mapped type, you can then check for each KeyType of ObjectType...

import type {RemoveIndexSignature} from 'type-fest';

type RemoveIndexSignature<ObjectType> = {
	[KeyType in keyof ObjectType // Map each key of `ObjectType`...
	]: ObjectType[KeyType]; // ...to its original value, i.e. `RemoveIndexSignature<Foo> == Foo`.
};

...whether an empty object ({}) would be assignable to an object with that KeyType (Record<KeyType, unknown>)...

import type {RemoveIndexSignature} from 'type-fest';

type RemoveIndexSignature<ObjectType> = {
	[KeyType in keyof ObjectType
		// Is `{}` assignable to `Record<KeyType, unknown>`?
		as {} extends Record<KeyType, unknown>
			? ... // ✅ `{}` is assignable to `Record<KeyType, unknown>`
			: ... // ❌ `{}` is NOT assignable to `Record<KeyType, unknown>`
	]: ObjectType[KeyType];
};

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.

import type {RemoveIndexSignature} from 'type-fest';

type RemoveIndexSignature<ObjectType> = {
	[KeyType in keyof ObjectType
		as {} extends Record<KeyType, unknown>
			? never // => Remove this `KeyType`.
			: KeyType // => Keep this `KeyType` as it is.
	]: ObjectType[KeyType];
};

Examples

Example 1

import type {RemoveIndexSignature} from 'type-fest';

interface Example {
	// These index signatures will be removed.
	[x: string]: any
	[x: number]: any
	[x: symbol]: any
	[x: `head-${string}`]: string
	[x: `${string}-tail`]: string
	[x: `head-${string}-tail`]: string
	[x: `${bigint}`]: string
	[x: `embedded-${number}`]: string

	// These explicitly defined keys will remain.
	foo: 'bar';
	qux?: 'baz';
}

type ExampleWithoutIndexSignatures = RemoveIndexSignature<Example>;
// => { foo: 'bar'; qux?: 'baz' | undefined; }

Type Parameters

ObjectType
definition: [KeyType in keyof ObjectTypein keyof { } extends Record<KeyType, unknown> ? never : KeyType]: ObjectType[KeyType]