Skip to content
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

Properties prefixed with double-underscore cannot be accessed in mapped type mappings #15334

Closed
jchurchill opened this issue Apr 23, 2017 · 1 comment · Fixed by #16915
Closed
Assignees
Labels
Bug A bug in TypeScript

Comments

@jchurchill
Copy link

TypeScript Version: 2.2.2
Also present in the current nightly build 2.3.0-dev.20170423.

Code

interface Properties {
    property1: string;
    __property2: string;
}

// As expected, I can make an object satisfying this interface
const ok: Properties = {
    property1: "",
    __property2: ""
}

// As expected, "__property2" is indeed a key of the type
type Keys = keyof Properties;
const k: Keys = "__property2"; // ok

// Unexpected: it cannot be accessed in the type mapping.
// Error: "Property '__property2' does not exist on type 'Properties'."
type Property2Type = Properties["__property2"];

// This results in unexpected behavior with Partial and other things that rely on mapped types.
const partial: Partial<Properties> = {
    property1: "",
    __property2: "" // Error: Object literal may only specify known properties
}

Expected behavior:
Regarding Mapped Type semantics, properties prefixed with two underscores behave the same as those without.

Actual behavior:
Properties prefixed with two underscores seem to be present in keyof T, but their corresponding entry is not present in the type mapping T[Key].

Alternatively...
Alternatively, if there's some limitation about double-underscore-prefixed member names I'm not aware of, it would seem acceptable to document that mapped types do not map over properties prefixed with two underscores __, and to have keyof T be consistent with that by not including such properties.

@jchurchill
Copy link
Author

Just wanted to say thank you for following up here! I noticed the fix and was able to remove my gross type hacks to get around it. 😄

@microsoft microsoft locked and limited conversation to collaborators Jul 3, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants