0

I have input data in this form:

const input = {
    postcode: ['0000']
    submitted: ['yes']
}

const output = {
    postcode: '0000'
    submitted: 'yes'
}

How to create interface for input data?

I tried:

interface inputData {
    postcode: string[]
    submitted: string[]
}

interface outputData {
    postcode: string
    submitted: string
}

But I get error while applying it:

const extract = (input: inputData[]): outputData => {
  for (const key in input) {
    if (Object.hasOwnProperty.call(input, key)) {
      input[key] = input[key][0]; // Error here
    }
  }
  const output: any = input;
  return output;
}

Error: Element implicitly has an 'any' type because expression of type '0' can't be used to index type 'inputData'. Property '0' does not exist on type 'inputData'.

4
  • :outputData isnt a type (its a variable) What are you expecting the output to look like? Commented Jun 13, 2022 at 20:51
  • its still not clear, in your first paragraph you say you have input data in the form... and you define a const called outputData. and your output doesnt have the type of outputData. Did you mean `const output = { postcode: '0000' submitted: 'yes' } ? Commented Jun 13, 2022 at 20:55
  • Looks like your input and output data structure looks same, so you can create one interface and use that interface for input and output both. Commented Jun 13, 2022 at 20:59
  • @DamianGreen My bad. updated the question again Commented Jun 13, 2022 at 20:59

3 Answers 3

1

Here is a working version

function hasOwnProperty<X extends {}, Y extends PropertyKey>
  (obj: X, prop: Y): obj is X & Record<Y, unknown> {
  return obj.hasOwnProperty(prop)
}

interface inputData {
    postcode: string[];
    submitted: string[];
}
type outputData = inputData;

const extract = (input: inputData): outputData => {
    for (const key in input) {
        if (hasOwnProperty(input, key)) {
            const value = input[key];

            if (value instanceof Array) {
                input[key] = value[0];
            }
        }
    }
    const output: any = input;
    return output;
}
Sign up to request clarification or add additional context in comments.

Comments

1

From the information you've provided the type of inputData is Record<string,string[]>` Record is a utility type (Link)

    postcode: ['0000'],
    submitted: ['yes']
}

type inputDataT = Record<string,string[]>

interface inputData {
    postcode: string[]
    submitted: string[]
}

const extract = (input: inputDataT) => {
  const output: any = input;
  for (const key in input) {
    if (Object.hasOwnProperty.call(input, key)) {
      output[key] = input[key][0];
    }
  }

  return output;
}
console.log(extract(inputData))

Also if you're sure that there will only ever be one string in the property value array you can make it stronger as follows:

type inputDataT = Record<string,[string]>
const inputData:inputDataT = {
    postcode: ['0000'],
    submitted: ['yes']
}

Playground

5 Comments

Can you please tell what is Record?
I can actually only use this type inputDataT = Record<string,string[]> and it still works but looking at interface, it doesn't represent what are the types inside the record..
it matches the type in your question, please update the question with actual data if it differs
Yes but what if someone pass the input as postcodeBlahBlah: ['0000'], will it still validate? it has be only postcode: ['0000'] right?
I've posted 2 different ways of validating against your data. Without knowing the exact schema it's not possibel to know the exact format.
0

You can define an interface with an indexer:

interface EnumServiceGetOrderBy {
    [index: number]: { id: number; label: string; key: any };
}

1 Comment

How is this the right approach? What's EnumServiceGetOrderBy. where are id, label and key mentioned in the question?

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.