Verify in typescript if type A is equal to either type B or type C

Within one specific file, there is a structured code block like the following:

export const _total = {
  a: '',
  b: '',
  c: '',
  d: '',
  e: '',
  f: '',
}
type TotalKeysType = typeof _all;
export type TotalKey = keyof TotalKeysType;

In a separate file, there exists the following code structure:

export const _retain = {
  a: '',
  b: '',
  d: '',
  e: '',
}
type RetainKeysType = typeof _retain;
export type RetainKey = keyof RetainKeysType;

export const _deny = {
  c: '',
  f: '',
}
type DenyKeysType = typeof _deny;
export type DenyKey = keyof DenyKeysType;

Is it possible to utilize Typescript in guaranteeing that the keys specified in _total are always equivalent to the combination of keys from both _retain and _deny? Essentially, ensuring that TotalKey always matches RetainKey | DenyKey.

The intention here is to have the Typescript compiler raise an error if a developer adds a new value (for instance, z) to _total, while neglecting to include z within either _retain or _deny.

Answer №1

To achieve this, you can create a conditional type that will determine if two types are equal or not by resolving to true when they match, and false otherwise. By implementing code that triggers a compile error when the type is not true, you can ensure synchronization between the types.

If any changes occur in either of the types, a compile error will be thrown, prompting you to update the mismatched type. This approach proves especially handy when tracking modifications to a type within a separate library.

For instance:

type IsExact<T, U> = [T] extends [U] ? [U] extends [T] ? true : false : false;
function assert<T extends true | false>(expectTrue: T) {}

// trigger a compile error if types fall out of sync
assert<IsExact<AllKey, KeepKey | IgnoreKey>>(true);

Though the code may appear lengthy (e.g., managing the any type), it's conveniently packaged in my library found here.

import { assert, IsExact } from "conditional-type-checks";

// define AllKey, KeepKey, IgnoreKey or import them

assert<IsExact<AllKey, KeepKey | IgnoreKey>>(true);

Another Approach

An alternative albeit less elegant method involves creating two objects representing each type and assigning them to one another.

() => {
  let allKeys: AllKey;
  let otherKeys: KeepKey | IgnoreKey;

  // use lambdas to prevent type modification on assignment
  () => allKeys = otherKeys;
  () => otherKeys = allKeys;
};

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Define an object by extracting properties from an array of objects

Is there a way to refactor this code to remove the need for explicit casting? type A={a:number}; type B={b:string}; let a:A={a:0}; let b:B={b:''}; function arrayToObject<T>(array:T[]):T { return array.reduce((r,c) =>Object.assign ...

Issue with TypeScript: Declaring type for objects in an array using .map

I'm having trouble assigning types to the Item object that is being returned from unitedStates.map((item: Item) => {}. Despite my efforts, I am unable to correctly define the types. Although I have specified the unitedStates array of objects as un ...

An error occurred when trying to access object references within a function

Here is a snippet of my code: const fn1 = (param1: string, param2: string, param3: string): Promise<void> => {return new Promise()} const fn2 = (param1: string, param2: string): void => {return} const options = { option1: fn1, option2: ...

Firestore rule rejecting a request that was meant to be approved

Recently, I came across some TypeScript React code that fetches a firestore collection using react-firebase-hooks. Here's the snippet: const [membersSnapshot, loading, error] = useCollectionData( query( collection(db, USERS_COLLECTION).withConve ...

How to Retrieve Inputs from Child Component Form without Prop Passing?

Within the Parent component below, there is a Dropdown menu with two options. Selecting "TOP LEVEL" will display Form1, while selecting "MAKE ITEM" will show Form2. If no option is selected, both forms remain hidden. The Parent component also contains a Bu ...

Problem encountered with @HostListener

In an Angular component, I have the following code snippet that is functioning as intended: @HostListener('document:click', ['$event']) onClick(event) { if(!this.eRef.nativeElement.contains(event.target)) { console.log("clicked out ...

Guide to setting up react-styleguidist, developing with Create React App, using Typescript, incorporating Material UI, and including

Struggling to configure react-styleguidist (RSG) with Create React App 3 (CRA), Typescript, Material UI, and styled-components. Currently encountering an error: ./node_modules/react-styleguidist/lib/client/rsg-components/ReactExample/ReactExample.js Modul ...

How can TypeScript objects be serialized?

Is there a reliable method for preserving type information during JSON serialization/deserialization of Typescript objects? The straightforward JSON.parse(JSON.stringify) approach has proven to have several limitations. Are there more effective ad-hoc sol ...

Obtain a tuple of identical length from a function

I'm attempting to create a function that returns a tuple with the same length as the parameter tuple passed to it. Although I tried using generics, I am encountering an error when applying the spread operator on the result. My goal is best illustrate ...

What is the method for defining a monkey patched class in a TypeScript declarations file?

Let's say there is a class called X: class X { constructor(); performAction(): void; } Now, we have another class named Y where we want to include an object of class X as a property: class Y { xProperty: X; } How do we go about defining ...

Utilizing Angular's ngx-bootstrap date range picker for a customized date range filtering system

Currently, I am incorporating ngx-bootstrap's datepicker functionality and utilizing the date range picker. This feature allows users to choose a start and end date. After selecting these dates, my goal is to filter the array(content) based on whethe ...

Material-UI React Native components: Injecting Styles without Props

import {createStyles, WithStyles} from "@material-ui/core"; const styles = (theme: Theme) => createStyles({ root: {} }); interface MyProps extends WithStyles<typeof styles> { } export class MyComponent extends Component<MyProps ...

The type 'HTMLDivElement | null' cannot be assigned to the type 'HTMLDivElement' in this context

Struggling with a scroll function to maintain position while scrolling up or down - encountering an error: Error: Type 'HTMLDivElement | null' is not assignable to type 'HTMLDivElement'. Type 'null' is not assignable to type & ...

Is it considered best practice to include try/catch blocks within the subscribe function in RxJs?

Imagine we have an Angular 2 application. There is a service method that returns data using post(), with a catch() statement to handle any errors. In the component, we are subscribing to the Observable's data: .subscribe( ()=> { ...

Best practices for applying the Repository pattern within a NestJS application

After reviewing the NestJS documentation and examining their sample source codes, it appears challenging to implement a Repository pattern between the service layer and the database layer (e.g. MongoDB). In NestJS, database operations are executed directl ...

Using an external module in a Vue SFC: a beginner's guide

Recently delving into Vue, I'm working on constructing an app that incorporates Typescript and the vue-property-decorator. Venturing into using external modules within a Single File Component (SFC), my aim is to design a calendar component utilizing t ...

Unable to resolve external modules in TypeScript when using node.js

I wanted to integrate moment.js into my node application, so I proceeded by installing it using npm: npm install <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="adc0c2c0c8c3d9ed9f8399839d">[email protected]</a> J ...

Declaring a function in TypeScript triggers an error notification

I encountered a typescript error while running my code stating that "require" is not a function. To resolve this, I declared the function beforehand; however, a new typescript error now occurs saying "Modifiers cannot appear here." Can anyone assist me w ...

Angular HTML Component Refactor causes compatibility issues with BS4 classes

Currently, I am working on Angular components and I have a specific section that I would like to refactor into a separate component for reusability. Initially, when the HTML block with only Bootstrap 4 classes is placed in the parent component, the user in ...

Using Rxjs to reset an observable with combineLatest

My scenario involves 4 different observables being combined using "combineLatest". I am looking for a way to reset the value of observable 2 if observables 1, 3, or 4 emit a value. Is this possible? Thank you! Mat-table sort change event (Sort class) Mat- ...