What causes the second parameter of the function to always appear as "never"?

I am looking to create a simple generic interface for a function that accepts an object's interface, where the first argument is the key of the object and the second argument is another key within that object.

Here is my proposed solution:

type Test<T extends Object, K extends keyof T = keyof T, V extends keyof T[K] = keyof T[K]> = (key: K, key2: V) => void

Object:

const convertFunctionsMap = {
  ms: {
    h: millisecondsToHours,
    s: millisecondsToSeconds,
    m: millisecondsToMinutes
  },
  s: {
    h: secondsToHours,
    ms: secondsToMilliseconds,
    m: secondsToMinutes
  },
  m: {
    ms: minutesToMilliseconds,
    s: minutesToSeconds,
    h: minutesToHours
  },
  h: {
    ms: hoursToMilliseconds,
    s: hoursToSeconds,
    m: hoursToMinutes
  }
}

However, I encountered the following issue:

https://i.sstatic.net/NR0o0.jpg

CodeSandbox: https://codesandbox.io/s/empty-leaf-cuz51r?file=/src/index.ts

Answer №1

It's hard to put into words, but I have a feeling it will do the trick

interface Experiment<E> {
  <P1 extends keyof E, P2 extends keyof E[P1]>(param1: P1, param2: P2): void;
}

const transformationFunctions = {
  ms: {
    h: 1,
    s: 2,
    m: 3
  },
  s: {
    h: 1,
    ms: 2,
    m: 3
  },
  m: {
    ms: 1,
    s: 2,
    h: 3
  },
  h: {
    ms: 1,
    s: 2,
    m: 3
  }
}
const executeExperiment: Experiment<typeof transformationFunctions> = ()=> {}
executeExperiment('ms', 'h');

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

Ways to append each list item from one unordered list to the end of another based on their unique styles

I am in the process of making a website responsive and I am faced with the task of combining two different menus. In order to achieve this, I need to transfer all list items (li) from one unordered list (ul) to another. Provided below is a simplified vers ...

The prop type `cellHeight` provided to `GridList` in (Material-ui / React) is invalid

warning.js:33 Warning: The prop type for cellHeight in the GridList component is invalid. I encountered this error message, despite the property functioning correctly. Is there a way to resolve this issue? If you're interested, check out the documen ...

Does NextJS come with an index.html template page pre-installed?

Today, I delved into NextJS for the first time as I transitioned a ReactJS website over to it. While I find it to be a powerful framework, there is one particular feature that seems to be missing. In traditional ReactJS (without the NextJS framework), we ...

Attempting to implement a b-table that can manage multiple arrays within an array

This is the b-table, designed to display a column of food items <b-table :fields="Fields" :items="ITEMS"> <template #cell(food)="data"> {{data.item.food}} </template> </b-table> //Column ...

Looking to Move the Image Up?

I've been experimenting with creating a grid layout where the bottom image will move up until it is 10px away from the image directly above it. However, no matter what I attempt, the spacing seems to be based on the tallest image rather than the one a ...

What strategies can I implement to prevent the JavaScript CallStack from becoming overloaded?

The code snippet below outlines the functionality achieved through JavaScript (specifically for a node.js COMET application): A request is sent to the server and held until there is a response. Upon receiving a response, the data is processed, and anothe ...

Using Array.push to add an object retrieved from a Redis cache in a Node.js application is causing issues and is not functioning as expected

I've encountered a problem with retrieving all keys from my Redis cache, storing them in an array, and sending that array to the user using Express. The issue arises when I receive an empty array as the response with no objects in it. I attempted to u ...

Is there a way to make my JavaScript code function within an ASP.NET CSHTML file?

I am working on a web project where I have an HTML input for uploading a picture to my FTP server. I want to use JavaScript to detect changes on my upload button. This is the code in my CSHTML file: @using (Html.BeginForm("UploadFile", "Upload", For ...

AJAX POST function behaving more like a GET request

I've encountered an issue while trying to send contact page data to my view. It was working perfectly until I added if else statements. Below is the script I'm using: <script> function Submit() { var name = document.getElementById(&a ...

Using ng-options to connect an array of objects to an ng-init attribute

Working on creating a select dropdown using ng-options to iterate over an array of objects and linking it to ng-init. However, running into an issue where the correct variable needs to be passed through ng-change as well. The current code is set up in a w ...

AngularJS options for selecting items: radio buttons and checkboxes

I am currently working on creating a concatenated string based on the selection of radio buttons and checkboxes. There are two radio button groups and one checkbox group. One of the radio button groups is functioning correctly, but the other automatically ...

Return all HTML code from the Ajax element

I can't seem to pinpoint the issue with my code. When I make an ajax call using the code below: ajax.js: function ajaxObj(meth, url){ var x = new XMLHttpRequest(); x.open(meth, url, true); x.setRequestHeader("Content-type", "application/x-www_form-u ...

Implementing real-time data updates on a webpage using Node.js

Currently, I am faced with the challenge of updating values on my page in real-time without the need for constant webpage refreshing. To illustrate, consider a scenario where a user filters a real estate website by location and receives results showing the ...

Ensuring the Presence of a Legitimate Instance in NestJS

I have been working on validating my request with the Product entity DTO. Everything seems to be in order, except for the 'From' and 'To' fields. The validation works correctly for the Customer and Type fields, but when incorrect data i ...

Is the conditional module require within an "If" statement always executed in nuxt.config.js?

Despite the missing file mod.json, the "require" statement is still executed leading to an error. The condition should have prevented it from entering the "if" block: const a = 1; if(a === 2){ const mod = require('./scripts/mod ...

Sending user credentials to a new page in JavaScript

My goal is to direct the user from one web page to another that requires a password for access. The user arrives on the initial page by scanning a barcode with a specific terminal, similar to a smartphone equipped with a customized barcode reader applicati ...

What is the best way to prevent updating the state before the selection of the end date in a date range using react-datepicker?

Managing user input values in my application to render a chart has been a bit tricky. Users select a start date, an end date, and another parameter to generate the chart. The issue arises when users need to edit the dates using react-datepicker. When the s ...

What is the best way to send multiple data using GetServerSideProps?

I have a challenge where I need to pass multiple sets of sanity data into a component, but I am restricted to using getServerSideProps only once. How can I work around this limitation to include more than one set of sanity data? pages > members.tsx exp ...

Having trouble with rendering an OBJ file in threejs

It appears that some faces are missing from the obj file, resulting in improper rendering. The same object viewed in an online viewer shows all faces and parts rendered correctly. https://i.sstatic.net/tI3kb.jpg Below is the image I am trying to render on ...

Challenges Arising from CGI Scripts

One requirement for the user is to input text into a designated text field within a form element in my HTML. Following this, a CGI script processes the user's input and JavaScript code is responsible for displaying the processed information. JavaScri ...