简体   繁体   中英

Define an empty object type in TypeScript

I'm looking for ways to define an empty object type that can't hold any values.

type EmptyObject = {}

const MyObject: EmptyObject = {
  thisShouldNotWork: {},
};

Objects with the type are free to add any properties. How can I force MyObject to always be an empty object instead?

My actual use case is using the EmptyObject type inside an interface.

interface SchemaWithEmptyObject {
  emptyObj: EmptyObject; 
}
type EmptyObject = {
    [K in any] : never
}

const one: EmptyObject = {}; // yes ok
const two: EmptyObject = {a: 1}; // error

What we are saying here is that all eventual properties of our EmptyObject can be only never , and as never has no representing value, creating such property is not possible, therefor the object will remain empty, as this is the only way we can create it without compilation error.

type EmptyObject = Record<any, never>

这相当于Maciej Sikora 的答案,但使用了Record 实用程序类型

根据 VSC type emtyObj = Record<string, never>

There are several options for defining an empty object type, and it completely depends on the side-effects you want regarding your linter and typescript errors when accessing or setting properties. These are the options I tried:

// Option A:
let objA: Record<any, never> = {}; // Typescript-ESLint error: Unexpected any. Specify a different type.
objA = { prop: 'value' }; // Typescript error: Type 'string' is not assignable to type 'never'.
console.log(objA.prop);
console.log(objA.nonExistingProp); // No error!!!

// Option B:
let objB: Record<string, never> = {};
objB = { prop: 'value' }; // Typescript error: Type 'string' is not assignable to type 'never'.
console.log(objB.prop);
console.log(objB.nonExistingProp); // No error!!!

// Option C:
let objC: Record<never, never> = {};
objC = { prop: 'value' };
console.log(objC.prop); // Typescript error: Property 'prop' does not exist on type 'Record<never, never>'
console.log(objC.nonExistingProp); // Typescript error: Property 'nonExistingProp' does not exist on type 'Record<never, never>'.

TLDR: Record<string, never> raises errors when setting properties to the empty object. Record<never, never> raises errors when accessing properties of the empty object. I have yet to find a solution that raises errors in both cases.

Personally I went with option C for my current use-case, because I wanted an error to happen if I try to access a property of an empty object, but you may not want that!

How about something like this with a private symbol?

// EmptyType.ts
const EMPTY_SYMBOL = Symbol();
export type EmptyType = {[EMPTY_SYMBOL]?: never};

It works correctly against an intersection union type.

import {EmptyType} from "./EmptyType";
type Union = EmptyType | { id: string };
const a: Union = {};
const b: string = a.id; // <-- error

TypeScript Playground

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM