Skip to content

Statically guarantee presence of object key via string literal type #33410

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
5 tasks done
rix0rrr opened this issue Sep 13, 2019 · 5 comments
Closed
5 tasks done

Statically guarantee presence of object key via string literal type #33410

rix0rrr opened this issue Sep 13, 2019 · 5 comments
Labels
Duplicate An existing issue was already created

Comments

@rix0rrr
Copy link

rix0rrr commented Sep 13, 2019

Search Terms

object key string literal

Suggestion

I'm trying to write a function to set a field in an object using a string literal type. I can write the signature for it, but I can't write the body. A simplified version of it looks like this:

function foo1<N extends string>(key: N): {[key in N]: string} {
    return { [key]: 'foo' };
//  ~~~~~~~~~~~~~~~~~~~~~~~~
//  Type '{ [x: string]: string }' is not assignable to type '{[key in N: string}'
}


function foo2<N extends string>(key: N): {[key in N]: string} {
    return { [key]: 'foo' } as any; // Cast away the error
}


const x = foo2('henk');   // x :: { henk: string } as expected

I imagine this has something to do with the keys of an object literal being declared to be string and the stronger type N extends string getting dropped on the floor?

But the code feels like it is correct and it feels like the type system should be able to see that.

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.
@jack-williams
Copy link
Collaborator

Keys in mapped type are related contravariantly (in reverse), so even though key is related to N, it's not the case that N is related to the particular key denoted by key. As an example of why it is correct to flag an error, consider this usage.

const a = foo1<'x' | 'y'>('x');
const y: string = a.y // not really a string.

@poseidonCore
Copy link

The best that you could do is some constructive process, but this just illustrates the issue noted above:

function foo1<N extends string>(key: N): {[key in N]: string} {
    var result:{
        [key in N]: string
    } = <any>{}; 
   // ...
   // You would need to construct `result` so that it had all the keys of N, being passed by one of them.
    return result;
}

const x = foo1('henk');   // x = { henk: string }
const a = foo1<'x' | 'y'>('x'); // a = {x: string; y: string; }

@rix0rrr
Copy link
Author

rix0rrr commented Sep 13, 2019

That's a fair point. I don't suppose there is a way to restrict N to not be a union?

@jack-williams
Copy link
Collaborator

jack-williams commented Sep 13, 2019

Related: #24085, #27808, #28430

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Sep 13, 2019
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

5 participants