Skip to main content
Module

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

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

Create a type that does not allow extra properties, meaning it only allows properties that are explicitly declared.

This is useful for function type-guarding to reject arguments with excess properties. Due to the nature of TypeScript, it does not complain if excess properties are provided unless the provided value is an object literal.

Please upvote this issue if you want to have this type as a built-in in TypeScript.*

Examples

Example 1

type OnlyAcceptName = {name: string};

function onlyAcceptName(args: OnlyAcceptName) {}

// TypeScript complains about excess properties when an object literal is provided.
onlyAcceptName({name: 'name', id: 1});
//=> `id` is excess

// TypeScript does not complain about excess properties when the provided value is a variable (not an object literal).
const invalidInput = {name: 'name', id: 1};
onlyAcceptName(invalidInput); // No errors

Having Exact allows TypeScript to reject excess properties.

Example 2

import {Exact} from 'type-fest';

type OnlyAcceptName = {name: string};

function onlyAcceptNameImproved<T extends Exact<OnlyAcceptName, T>>(args: T) {}

const invalidInput = {name: 'name', id: 1};
onlyAcceptNameImproved(invalidInput); // Compilation error

Read more

Type Parameters

ParameterType
InputType extends ParameterType
definition: ParameterType extends Primitive ? ParameterType : [Key in keyof ParameterType]: Exact<ParameterType[Key], InputType[Key]> & Record<Exclude<keyof InputType, KeysOfUnion<ParameterType>>, never>