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

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

Closed
gcnew opened this issue Aug 29, 2017 · 4 comments
Closed
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@gcnew
Copy link
Contributor

gcnew commented Aug 29, 2017

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.

@gcnew gcnew changed the title Global object properties should not surface on type level indexing [Suggestion] Global object properties should not surface on type level indexing Aug 29, 2017
@gcnew
Copy link
Contributor Author

gcnew commented Aug 29, 2017

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 

@mhegazy mhegazy added Bug A bug in TypeScript In Discussion Not yet reached consensus labels Aug 29, 2017
@olegdunkan
Copy link

#16578 (comment)

@mhegazy
Copy link
Contributor

mhegazy commented Nov 7, 2017

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.

@mhegazy mhegazy added Working as Intended The behavior described is the intended behavior; this is not a bug and removed Bug A bug in TypeScript In Discussion Not yet reached consensus labels Nov 7, 2017
@typescript-bot
Copy link
Collaborator

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

@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

4 participants