Steps for obtaining a fresh TS type

I've been diving into TypeScripe recently and I'm working on creating some useful ts types.

For instance:

Here's an interface I've come up with:

interface Student{
  name: string,
  id: number,
  gender: string
 }

Now, I need to extract the keys from this interface to create a new Array type like so

type Arr = ['name', 'id', 'gender']

Any ideas on how I can write a utility type to achieve this?

Answer №1

Here is the solution you've been searching for: . For additional solutions, check out this link https://github.com/microsoft/TypeScript/issues/13298.

To address your specific issue, utilizing the keyof keyword to obtain a union of keys (keyof Student) is necessary, followed by passing it to the TupleUnion helper function. It's important to note that the sequence of literal values in the union may not always be predictable.

Explore more at https://tsplay.dev/Nr922w

Answer №2

It puzzles me why others are complicating it with tuples, unions, and so on. Please correct me if I'm mistaken, but my approach has always been straightforward and effective without any problems:

const keys: (keyof Student)[] = [];

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

Nuxt VueJS has various modules with names that have only minor differences in letter casing

I encountered an error while compiling with nuxtJS and couldn't find a solution on Google. The error seems to be related to declaring capital letters, but it is actually happening in the node_modules directory rather than in my components. [HMR] bund ...

Unable to dispatch JWT in headers to the server

Currently, I am in the process of learning how to develop a fullstack application with Vue.js as the front-end framework, Express as the back-end server, and Mongodb for database management. I have implemented a login form and performed basic authenticatio ...

TS-2304 Error - 'Iterable' not found in TypeScript when trying to import 'jquery' into a '.ts' file

Currently, I am utilizing TypeScript version 2.4 in Visual Studio Code for development. My approach involved installing jQuery via NPM using the given command: npm install --save @types/jquery Subsequently, I obtained the source code for the jquery modul ...

Creating a JSON utility type for an already existing type, such as JSON<SomeExistingType>

Is there any tool or utility available that can accomplish this task? const foo: Foo = { ... } const bar: string = JSON.stringify(foo) const baz: JSON<Foo> = JSON.parse(foo) JSON<Foo> would essentially mirror all the properties of Foo, with th ...

Incorrect cluster locations of MapBox supercluster

I am currently working on creating a clustered map using mapbox supercluster. However, I am encountering an issue where the clusters are appearing in incorrect locations. For instance, even though I only have data for dogs in the Netherlands, when I zoom o ...

Clicking the button fails to trigger the modal popup

Upon clicking a button, I am attempting to open a modal popup but encountering an error: The button click works, however, the popup does not appear after the event. test.only('Create Template', async({ page })=>{ await page.goto('h ...

Is there a way to trigger the vue-cli build command automatically when specific files are modified?

One way I can build my JavaScript file is by using the following command: vue build main.js --dist dist --prod --lib While this works, I am looking for a way to automate this process whenever I make changes to my JS or Vue files. I prefer not to rely on ...

The image source is visible in Vue.js, but unfortunately, my picture is not showing up

Below is the code and script that I have: <template> <div class="tasks_container"> <div class="tasks_content"> <h1>Tasks</h1> <ul class="tasks_list"> ...

Is it possible for Promise.all to accept an array containing only non-Promise elements?

Promise.all typically resolves when all promises in its array resolve. However, there is an instance where one element of the input array is not a promise https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise/all#Using_Promi ...

A guide on accessing a setup() variable within an imported function

I have a scenario where I am trying to utilize a reactive variable defined within a component in an imported function. The code snippet below showcases a simple example of what I am trying to achieve: which is available in the playground App.vue <scrip ...

Is it possible to modify a single value in a React useState holding an object while assigning a new value to the others?

In my current state, I have the following setup: const [clickColumn, setClickColumn] = useState({ name: 0, tasks: 0, partner: 0, riskFactor: 0, legalForm: 0, foundationYear: 0 }) Consider this scenario where I only want to update ...

Issues with responsiveness and calculated attributes in Vue 3 using the Composition API

Within this menu, there are different items: Item 1 (marked as number 1 in orange) with the path: http://localhost:8080/#/documents Item 2 (marked as number 2 in orange) with the path: http://localhost:8080/#/documents/1 Item 3 (marked as number 3 in or ...

Error [ERR_ASSERTION]: Requested handler for GET /test is missing or undefined

I am currently facing an issue with the controller function in my TypeScript code. Although the handler function is present in the controller file, it is still giving me an error. export const test = { options:{ handler: (request, reply) => { ...

PDF viewing problem in iOS 11.3 when accessing PWA

I have successfully integrated Vuejs and Framework7 into my PWA. I am currently facing a challenge where I need to open a remote PDF file in my PWA while ensuring that users can easily navigate back to the PWA after viewing the PDF. The solution I initiall ...

Having trouble utilizing props with Vue axios? Running into an undefined error? Unsure how to properly use props with axios?

https://i.stack.imgur.com/QfCDG.png There seems to be an issue with my saveComment() function in CommentList.vue. It can't find the comments' post_id and causes this error: CommentList.vue?6c27:107 Uncaught TypeError: Cannot read properties of u ...

Steps to populate forms with data sourced from an API and ensure form validation using Element UI in Vue

After obtaining data from an API, I need users to fill out a form with validation using the Element-UI el-form component. Here is the form structure: Template: <el-form ref="form" :model="form" :rules="formRules"> <el-form-item :prop="companyNa ...

Harnessing the power of Bootstrap 5 alongside VueJS 3

I have been delving into the world of Vue.js and Bootstrap lately. I recently discovered that you can integrate Bootstrap with Vue.js, so I decided to give it a shot. My code is fairly straightforward - just a dropdown and a popover. While the dropdown is ...

Display the chosen HTML template in real-time

Recently, I started using Angular 2 and encountered an issue that I need help with. 1] I have created 2-3 templates for emails and SMS that will display predefined data. 2] I have also designed a screen with a dropdown menu containing options like email ...

Implementing email rendering with Vue 3 on an express server

Utilizing Vue for our frontend has been a great success, and now I'm looking to integrate it into our email templates on the Node (Express.js) server. Thanks to @vue/server-renderer, this process generally works smoothly. Currently, sending an email f ...

Upon clicking, Vue triggers form validation in Laravel

I have encountered an issue that I am unable to solve by myself: Within my Laravel view, I have included a form in the following manner {!! Form::open(array('route' => ['reports.store', $project->id], 'data-parsley-validate& ...