0

I have an object with some concrete fields:

 const obj1: Obj = {  
    hello: true, 
    second: false, 
    third: 311,  
    fifty: 50  
    .....  
}  

I need to map some fields from this object to concrete number, (and use it value) and return array with it:

interface ArrayItem {
    index: number;
    visible: boolean;
}

// hello should be mapped to 3, and visible - 'true'
// result is:
const arr: ArrayItem[] = [{index: 3, visible: true}]

For concrete field. e.g. if obj1, has field "hello", result will be:

[  
    {
     index: 3  
     visible: (value of 'hello', so -> true)  
    }  
]  

More examples:

 const obj1: Obj = {
    hello: true,
    second: false,
    third: 311,
    fifty: 50
    .....
}

// fields, which i looking:

const mapped = {
    hello: 3, // index value
    second: 5 // index value
}

output:

[
    {
     index: 3
     visible: (value of 'hello', so -> true)
    }
    {
     index: 5
     visible: (value of 'second', so -> false)
    }
]
4
  • So you want to parse Obj into new one only with index and visible keys, don't you? Commented Mar 20, 2019 at 15:16
  • that's correct. But depends on concrete fields. e.g. i know, that "hello" field will be transformed to 3. Commented Mar 20, 2019 at 15:18
  • Ok and how about Obj it must contains hello and second fields with booleans? And will any other keys affect on ArrayItem? Commented Mar 20, 2019 at 15:24
  • yes, they boolean. Obj has any other fields, which i do not need to handle. I have map with concrete fields which i need to map e.g. const maps = { hello: 3, second: 4 } Commented Mar 20, 2019 at 15:33

1 Answer 1

1

Let's declare type definition of Obj

type Obj = { [key: string]: boolean };

Object with many key, and bool on all values.

Array items look like this:

interface ArrayItem {
    index: number;
    visible: boolean;
}

But we can get some more knowledge about types based on values from Obj. To declare that let's write InterfaceParser generic.

type InterfaceParser<T extends Obj> = {
  index: T['hello'] extends true ? 3 : number;
  visible: boolean;
}

T extends Obj means that input type must be at least Obj. extends true ? 3 : number means that when hello key is true the type of index is not number is 3.

Playground

Playground2

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

4 Comments

thanks @Przemyslaw Pietrzak, but seems its not what i need. I updated main topic, have a look please.
To be honest i don't understand where is problem. I thought you want to have typed some values (e.g. 3 instead of number when 'hello'). Could you elaborate more, or give more exaples?
Updated topic. See below "More examples:" @Przemyslaw Pietrzak,
I think you're looking for something like: click Playground2 link

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.