2

I am having two files,

App.tsx:

const data = {games:{type: [], sport: [], category: []}}

The above is the dynamic data I receive .

I am in the need to set the above keyof typeof data.games to the type valid like,

type Valid = keyof typeof data.games

If it is same file then no issues to assign like the above.

But I have type file separately like,

Apptype.ts

// How can I make the below keyof typeof [dynamicName] ?
export type Valid = keyof typeof data.games;

export interface IOption {
  id: number;
  value: string;
  valid: Record<Valid, number[]>;
}

In the above file data.games throw error because there will not be such variable name in this file.

So how can I assign the dynamic value to keyof typeof [.....]?

Working Example:

Edit disable-dependent-dropdown-option-in-reactjs (forked)

2
  • This just looks like a scoping issue. Either export data from where it's defined or define Valid in the same module and export that Commented Nov 29, 2022 at 4:14
  • @Phil, Thanks for your comment. Could you please add solution on the same as I am new to typescript? Commented Nov 29, 2022 at 4:40

1 Answer 1

1

You can export and import the data from the App.tsx. And import types from Apptype.ts using

App.tsx:

import type { IOption, IState } from "./Apptype";
// ...

Apptype.ts

import { data } from "./App";

export interface IState {
  [key: string]: number;
}

export type Valid = keyof typeof data.games;

export interface IOption {
  id: number;
  value: string;
  valid: Record<Valid, number[]>;
}

codesandbox

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.