Determining a segment of a constant object's type

Suppose I have a function that requires a specific interface

interface Foo {
  foo: number;
  bar: number;
  baz?: number;
  buz?: number;
}

const doFoo = (params: Foo) => { /* ... */ };

I also possess a constant containing default values for some properties of Foo:

const BASE_PROPERTIES = {
  foo: 42,
  baz: 9001,
};

doFoo({
  ...BASE_PROPERTIES,
  bar: -1000
});

This works as expected. However, I am curious if there is a way to indicate that BASE_PROPERTIES includes specific parts of Foo. This would enhance code editing and clarify the purpose of the constant. Essentially, similar to an automated Pick<>.

I could attempt

const BASE_PROPERTIES: Partial<Foo> = { /* ... */ }

However, this results in an error for foo when used in the spread operator because of

Type 'number | undefined' is not assignable to type 'number'
. Although I could resolve it with as any or as Foo, it comes at the risk of potentially forgetting to include all necessary properties.

Another option is to use pick

const BASE_PROPERTIES: Pick<Foo, 'foo' | 'baz'> = { /* ... */ }

Yet, this approach involves manual maintenance which can be cumbersome, especially for large objects, and does not provide intellisense support when adding new properties, thereby partially defeating its original purpose.

Is there a method in TypeScript to suggest that an object should adhere to an interface schema, while allowing the compiler to determine how the constant aligns with the interface?

Answer №1

If you are in search of the satisfies operator that was recently introduced in Typescript 4.9

const BASE_PROPERTIES = {
  foo: 42,
  baz: 9001,
} satisfies Partial<Foo>

This code ensures that the object BASE_PROPERTIES satisfies the type Partial<Foo> while maintaining the literal type.


Playground

Answer №2

To simplify the process, you can create a utility function like this:

function generateBasicProperties<T extends keyof Bar>(props: Pick<Bar, T>) {
  return props
}

This way, you will benefit from intellisense and type checking.

const BASIC_PROPERTIES = generateBasicProperties({
  bar: 456,
})

You can then easily merge the result with your doBar function.

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

Can we trust the accuracy of the official type definition for JSON.stringify?

Upon reviewing the official type definition for JSON.stringify, it appears that it states JSON.stringify always returns a string, even when passed undefined. interface JSON { stringify(value: any, /*...*/): undefined; } However, executing JSON.stringif ...

`A bug in my Angular 4 application causes a TypeError when hosted on IIS`

After hosting an Angular 4 app on IIS, it seems there is an issue when accessing the app from machines other than the ones used for development. An error message " 'Uncaught TypeError: undefined is not a function' common.es5.js:3084" appears on t ...

Updating the background color using typescript

Before transitioning to angular development, I had experience working with vanilla Javascript. I encountered a challenge when trying to modify the css properties of specific elements using Typescript. Unfortunately, the traditional approach used in Javascr ...

Properties of a child class are unable to be set from the constructor of the parent class

In my current Next.js project, I am utilizing the following code snippet and experiencing an issue where only n1 is logged: class A { // A: Model constructor(source){ Object.keys(source) .forEach(key => { if(!this[key]){ ...

The JSX component cannot be named 'Stack.Navigator' or used as such

Encountering a type issue with react navigation using Stack.Navigation or Stack.Group from createNativeStackNavigator The error message indicates that the types do not match with JSX.element. Specifically, it states: Type '{}' is not assignable ...

Tips for including an authorization token in an HTTP request

I encountered a 401 unauthorized error when trying to access my REST endpoint, likely due to the security measures I have implemented. I suspect that there might be an issue with how I am handling the HTTP headers. The application utilizes a Spring Boot b ...

The IDE is showing an error, but Jest is able to run it flawlessly

I recently created a Jest unit test for a TypeScript function called checkEmail, which internally uses showAlert. The showAlert function in the utils.ts file looks like this: export const showAlert = (message: string) => { toast(message); }; In my ...

Prevent selection of future dates and display them in a muted grey color in the p-calendar component

I am attempting to prevent users from selecting future dates and visually distinguish them by setting a grey color background. However, I am having trouble disabling the future dates while the grey color background is functioning correctly. Any ideas on ho ...

Specify the dependencies in the package.json file to ensure that the React package designed for React v17 is compatible with React v18 as well

Recently, I developed an npm package using React v17.0.2. The structure of the package.json file is as follows: { "name": "shoe-store", "version": "0.1.0", "private": true, "dependencies": ...

In Visual Studio, the .js.map files and .js files seem to be mysteriously hidden, leaving only the TypeScript .ts files visible

In the past, I utilized Visual Studio Code for Angular 2 development and had the ability to hide .js and .js.map files from the IDE. Now, I am working on a project using VS 2017 Professional with Typescript, Jasmine, Karma, and Angular 4. Task Runner, etc. ...

Why is the ionChange/ngModelChange function being triggered from an ion-checkbox even though I did not specifically call it?

I have an issue with my code involving the ion-datetime and ion-check-box. I want it so that when a date is selected, the checkbox should automatically be set to false. Similarly, if the checkbox is clicked, the ion-datetime value should be cleared. Link ...

Designing the File and Folder Organization for Next.js Frontend and AWS Cloud Development Kit (CDK) Backend

When it comes to creating websites with serverless backends, I've been thinking about the best practices for folder structure. Currently, my setup includes a Next.js frontend and an AWS CDK backend. The way I've structured the folders has the bac ...

Is it possible to determine if an HTML form has been modified?

Is there a way in typescript to determine if a form has been changed and return true or false accordingly? For example, if I have a first name field with the current value of "John" and then change it to "Johny", it should return true. But if I revert it b ...

React with TypeScript is throwing an error that says: "The type 'string' does not have any properties in common with the type 'CSSProperties'."

Currently encountering a challenge while using Typescript in conjunction with React. https://i.sstatic.net/tHkoJ.png ...

Signal a return type error when the provided element label does not correspond with an existing entity

I am working on a component that accepts three props: children (React elements), index, and label. The goal is for the component to return the child element at a specific index when index is passed, and to return the element with a specific label when la ...

Webpack and React.js: Additional loaders might be required to manage the output generated by these loaders

An error occurred while parsing the module in ./productFlow/index.tsx at line 3, column 12. The file was processed with the following loaders: * ./node_modules/awesome-typescript-loader/dist/entry.js. It seems like an additional loader may be needed to h ...

Issues with the "noImplicitAny" setting in TS compilation

Having the following code snippet: let z; z = 50; z = 'z'; paired with the configuration in my tsconfig.json file: { "compilerOptions": { "target": "es5", "module": "commonjs", "sourceMap": false, "noEmitOnError": true, " ...

Instantiate a fresh object using the new keyword followed by the Class constructor, and if desired,

I'm looking for a class that I can easily create new instances from and optionally assign properties using the constructor. For instance: class Person { name: string; age: number; constructor(props: {name?: string, age?: number}) { this.nam ...

Launch a TypeScript Node.js server on Heroku deployment platform

I'm having trouble deploying a Node.js server built with TypeScript on Heroku. Despite following various tutorials and searching through Stack Overflow for solutions, I can't seem to make it work. Here is the code I have in my tsconfig.json and p ...

After restoring my Windows system, I encountered an issue with locating the typescript.js module for my Angular app development. I am currently troubleshooting the build

My PC had some issues, so I decided to restore Windows to an older restoration point. However, after doing this, I encountered an error while trying to build my Angular App: C:\Udemy\AngularDeCeroAExpertoEdicion2021\03-paisesApp>npm run ...