Skip to content

[Suggestion] Global object properties should not surface on type level indexing #18118

Closed
@gcnew

Description

@gcnew

TypeScript Version: nightly (2.6.0-dev.20170829)

Code

type Point = { x: number, y: number }
type Mapped = Record<'x'|'y', number>
type IndexSignature = { [x: string]: never }

// All of the following have type: `() => Object`
type Test1 = Point['valueOf']
type Test2 = Mapped['valueOf'] 
type Test3 = IndexSignature['valueOf']

Expected behavior:
Test1, Test2 and Test3 should all be errors.

User provided types should be considered "exact". No properties should be inherited from Object. The current behaviour is of little use, however it's quite confusing and really gets in the way of type level programming.

Activity

changed the title [-]Global object properties should not surface on type level indexing[/-] [+][Suggestion] Global object properties should not surface on type level indexing[/+] on Aug 29, 2017
gcnew

gcnew commented on Aug 29, 2017

@gcnew
ContributorAuthor

The magic Object keys are not present in keyof expressions, causing non-uniform behaviour:

type ValueOf<T, K extends keyof T> = T[K]
type Test4 = ValueOf<1, 'constructor'> // error
type Test5 = 1['constructor']          // OK 
olegdunkan

olegdunkan commented on Aug 30, 2017

@olegdunkan
mhegazy

mhegazy commented on Nov 7, 2017

@mhegazy
Contributor

It is consistent with other uses of indexing.. e.g. indexing with numbers.. keyof T will return you only string, public, and own properties. I would argue that this is exactelly what you want.. you do not want toString to start showing up in all keyof T... that would be too verbose, and would be a breaking change..

On the flip side, not having it in keyof T does not change the fact that toString exists on all objects.. this is in a sense similar to indexing with numbers.

added
Working as IntendedThe behavior described is the intended behavior; this is not a bug
and removed
BugA bug in TypeScript
on Nov 7, 2017
typescript-bot

typescript-bot commented on Nov 21, 2017

@typescript-bot
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

locked and limited conversation to collaborators on Jun 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @olegdunkan@gcnew@mhegazy@typescript-bot

        Issue actions

          [Suggestion] Global object properties should not surface on type level indexing · Issue #18118 · microsoft/TypeScript