2

What I wanna do is so simple.

In the example below,

interface InterfaceA { a: string, b: number }
interface InterfaceB { c: keyof InterfaceA }

const testMethod = (test: InterfaceB) => {
      const TestObject: InterfaceA = { a: '', b: 1 }

      TestObject[test.c] = 
    }

causes 'Type 'any' is not assignable to type 'never'' error.

I thought that test.c can be assigned in TestObject since c is the key of Interface A.

How can I make things work?

2
  • could you please edit your question to include a complete and verifiable example. What you've currently provided isn't enough for us to help you. Using the typescript playground you can include a link to a working example in your question. Commented Jan 14, 2022 at 11:11
  • @Olian04 Thank u for your advice! I edited my question with a simpler one. Commented Jan 14, 2022 at 11:52

1 Answer 1

3

You are receiving this error because the type of test.c can not be narrowed down any more than keyof InterfaceA. In other words, if we try to assign to TestObject[test.c] typescript wont be able to determine if we need to assign a string or a number. So it determines that the type must be all valid types at the same time, in this case number & string. However no value can be both a number and a string at the same time, so the resulting type ends up being never (See this playground for an example).

We can solve this by helping typescript narrow down the actual type of test.c like this:

interface InterfaceA { 
    a: string, 
    b: number
}
interface InterfaceB { 
    c: keyof InterfaceA
}

const testMethod = (test: InterfaceB) => {
    const TestObject: InterfaceA = { a: '', b: 1 }

    switch (test.c) {
        case 'a':
            TestObject[test.c] = 'foo';
            break;
        case 'b':
            TestObject[test.c] = 42;
            break;
    }
}

playground

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.