0
function myFunc<T extends string>(key: T): Record<T, string> {
        return { [key]: 'asdf' };
    }

I get an error:

Type '{ [x: string]: string; }' is not assignable to type 'Record<T, string>'.ts(2322)

How to get rid of the error and enable auto-completion on the returned object?

P.S. Without using return { [key]: 'asdf' } as Record<T, string>

1 Answer 1

2

Move the signature into an overload:

function myFunc<T extends string>(key: T): Record<T, string>;
function myFunc(key: string) {
    return { [key]: 'asdf' };
}

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.