Skip to content

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

Closed
@jchurchill

Description

@jchurchill

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.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScript

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions