Repository navigation
Keyof doesn't see all apparent members #16578
Description
Activity
This is by design. The
keyofoperator only considers the declared or inherited properties of a type, not those that come from its apparent type.- addedBy DesignDeprecated - use "Working as Intended" or "Design Limitation" insteadDeprecated - use "Working as Intended" or "Design Limitation" instead
on Jun 16, 2017 Anders Hejlsberg (@ahejlsberg) ok, thanks.
Would you mind if ask a question.
Spec doesn't define a notion of the apparent type, only apparent members .From spec:
The apparent members of the primitive type String and all string literal types are the apparent members of the global interface type 'String'I can assume from spec that the global interface
Stringis a apparent type if so then the global interface type 'Function' is also a apparent type.For global interface
Stringit works, but for the globalFunctiondoesn't !!!check("", "atChar"); //ok
The question is what is the apparent type, what is a difference between
StringandFunctionand more interesting what is a motivation to differ (Number,StringandBoolean) and (Object,Function) global interfaces in the context of thekeyoftype query?I have made some investigations.
Fromtsccode follows that the apparent type of typeTis{}except:
forstringis global interface String,
fornumberis global interface Number,
forbooleanis global interface Boolean,
fortype parameteris constraint, if any,
...function getApparentType(type: Type): Type { const t = type.flags & TypeFlags.TypeVariable ? getBaseConstraintOfType(type) || emptyObjectType : type; return t.flags & TypeFlags.Intersection ? getApparentTypeOfIntersectionType(<IntersectionType>t) : t.flags & TypeFlags.StringLike ? globalStringType : t.flags & TypeFlags.NumberLike ? globalNumberType : t.flags & TypeFlags.BooleanLike ? globalBooleanType : t.flags & TypeFlags.ESSymbol ? getGlobalESSymbolType(/*reportErrors*/ languageVersion >= ScriptTarget.ES2015) : t.flags & TypeFlags.NonPrimitive ? emptyObjectType : t; }
The motivation to differ (Number, String and Boolean) and (Object, Function) is probably to prevent enumerating of native (intrinsic) properties of
ObjectandFunctionfor user types.- locked and limited conversation to collaborators
on Jun 14, 2018
TypeScript Version: playground
Code
Expected behavior:
An index type query keyof T yields the type of permitted property names for T
I expected that "bind" is permitted (as I assume, public and in some cases protected) and apparent members of type
{():void}Form spec:
The apparent members of an object type T are the combination of the following:
Actual behavior:
Error.
If it by design I am with great pleasure would like to know what is permitted property names?