Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
a8d6a45
Move nameType from SymbolLinks to TransientSymbol
ahejlsberg Apr 5, 2018
39bb93f
Move nameType to base Symbol, generate nameType properties where missing
ahejlsberg Apr 5, 2018
fc85ba9
Limit getLiteralTypeFromPropertyName to only return string-like types
ahejlsberg Apr 6, 2018
ccf20d3
Accept new baselines
ahejlsberg Apr 6, 2018
ff20f38
Add support for numbers and symbols in keyof (but keep it disabled)
ahejlsberg Apr 9, 2018
68ce69a
Move 'PropertyKey' from es2015.core.d.ts to es5.d.ts
ahejlsberg Apr 9, 2018
2c02195
Accept new baselines
ahejlsberg Apr 9, 2018
6d93f30
Enable 'keyof T' for full string | number | symbol
ahejlsberg Apr 9, 2018
0379666
Update tests
ahejlsberg Apr 9, 2018
b1545fe
Accept new baselines
ahejlsberg Apr 9, 2018
b40592c
String index signatures do not apply to symbols
ahejlsberg Apr 11, 2018
0a37cd3
Update test
ahejlsberg Apr 11, 2018
b746f8e
Accept new baselines
ahejlsberg Apr 11, 2018
c344e6d
Fixes and improvements to indexed access type relationships
ahejlsberg Apr 15, 2018
6c60f7e
Accept new baselines
ahejlsberg Apr 15, 2018
8cb515a
Add members for numeric and symbol named properties in mapped types
ahejlsberg Apr 16, 2018
16cd558
Merge branch 'master' into improveIndexTypes
ahejlsberg Apr 16, 2018
b11be80
Don't widen unique symbol types during type inference
ahejlsberg Apr 17, 2018
02534cc
Accept new baselines
ahejlsberg Apr 17, 2018
b14d389
For 'T extends { [x: string]: XXX }' constraint of T[keyof T] is XXX
ahejlsberg Apr 18, 2018
254782c
Accept new baselines
ahejlsberg Apr 18, 2018
9e4e215
Revise IndexType to have stringsOnly property
ahejlsberg Apr 18, 2018
5f0d880
Update test
ahejlsberg Apr 18, 2018
eb7bbfb
Properties with numeric names have numeric literal types in keyof T
ahejlsberg Apr 20, 2018
b38e42e
Accept new baselines
ahejlsberg Apr 20, 2018
652e493
Address CR feedback
ahejlsberg Apr 21, 2018
c7f55be
Accept new baselines
ahejlsberg Apr 21, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Revise IndexType to have stringsOnly property
  • Loading branch information
ahejlsberg committed Apr 18, 2018
commit 9e4e215a030d5931dbfcb666352d6226ab742bf7
33 changes: 17 additions & 16 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8191,16 +8191,17 @@ namespace ts {
return links.resolvedType;
}

function getIndexTypeForGenericType(type: InstantiableType | UnionOrIntersectionType, includeDeclaredTypes?: boolean) {
const cacheLocation = includeDeclaredTypes ? "resolvedDeclaredIndexType" : "resolvedIndexType";
if (!type[cacheLocation]) {
type[cacheLocation] = <IndexType>createType(TypeFlags.Index);
type[cacheLocation].type = type;
if (includeDeclaredTypes) {
type[cacheLocation].isDeclaredType = true;
}
}
return type[cacheLocation];
function createIndexType(type: InstantiableType | UnionOrIntersectionType, stringsOnly: boolean) {
const result = <IndexType>createType(TypeFlags.Index);
result.type = type;
result.stringsOnly = stringsOnly;
return result;
}

function getIndexTypeForGenericType(type: InstantiableType | UnionOrIntersectionType, stringsOnly: boolean) {
return stringsOnly ?
type.resolvedStringIndexType || (type.resolvedStringIndexType = createIndexType(type, /*stringsOnly*/ true)) :
type.resolvedIndexType || (type.resolvedIndexType = createIndexType(type, /*stringsOnly*/ false));
}

function getLiteralTypeFromPropertyName(prop: Symbol, include: TypeFlags) {
Expand All @@ -8225,13 +8226,13 @@ namespace ts {
return numberIndexInfo !== enumNumberIndexInfo ? numberIndexInfo : undefined;
}

function getIndexType(type: Type, includeDeclaredTypes?: boolean): Type {
return type.flags & TypeFlags.Intersection ? getUnionType(map((<IntersectionType>type).types, t => getIndexType(t, includeDeclaredTypes))) :
maybeTypeOfKind(type, TypeFlags.InstantiableNonPrimitive) ? getIndexTypeForGenericType(<InstantiableType | UnionOrIntersectionType>type, includeDeclaredTypes) :
function getIndexType(type: Type, stringsOnly = keyofStringsOnly): Type {
return type.flags & TypeFlags.Intersection ? getUnionType(map((<IntersectionType>type).types, t => getIndexType(t, stringsOnly))) :
maybeTypeOfKind(type, TypeFlags.InstantiableNonPrimitive) ? getIndexTypeForGenericType(<InstantiableType | UnionOrIntersectionType>type, stringsOnly) :
getObjectFlags(type) & ObjectFlags.Mapped ? getConstraintTypeFromMappedType(<MappedType>type) :
type === wildcardType ? wildcardType :
type.flags & TypeFlags.Any ? keyofConstraintType :
keyofStringsOnly ? getIndexInfoOfType(type, IndexKind.String) ? stringType : getLiteralTypeFromPropertyNames(type, TypeFlags.StringLiteral) :
stringsOnly ? getIndexInfoOfType(type, IndexKind.String) ? stringType : getLiteralTypeFromPropertyNames(type, TypeFlags.StringLiteral) :
getIndexInfoOfType(type, IndexKind.String) ? getUnionType([stringType, numberType, getLiteralTypeFromPropertyNames(type, TypeFlags.UniqueESSymbol)]) :
getNonEnumNumberIndexInfo(type) ? getUnionType([numberType, getLiteralTypeFromPropertyNames(type, TypeFlags.StringLiteral | TypeFlags.UniqueESSymbol)]) :
getLiteralTypeFromPropertyNames(type, TypeFlags.StringOrNumberLiteralOrUnique);
Expand Down Expand Up @@ -10438,7 +10439,7 @@ namespace ts {
// constraint of T.
const constraint = getConstraintForRelation((<IndexType>target).type);
if (constraint) {
if (result = isRelatedTo(source, getIndexType(constraint, (target as IndexType).isDeclaredType), reportErrors)) {
if (result = isRelatedTo(source, getIndexType(constraint, (target as IndexType).stringsOnly), reportErrors)) {
return result;
}
}
Expand Down Expand Up @@ -21010,7 +21011,7 @@ namespace ts {
// Check if the index type is assignable to 'keyof T' for the object type.
const objectType = (<IndexedAccessType>type).objectType;
const indexType = (<IndexedAccessType>type).indexType;
if (isTypeAssignableTo(indexType, getIndexType(objectType, /*includeDeclaredTypes*/ true))) {
if (isTypeAssignableTo(indexType, getIndexType(objectType, /*stringsOnly*/ false))) {
if (accessNode.kind === SyntaxKind.ElementAccessExpression && isAssignmentTarget(accessNode) &&
getObjectFlags(objectType) & ObjectFlags.Mapped && getMappedTypeModifiers(<MappedType>objectType) & MappedTypeModifiers.IncludeReadonly) {
error(accessNode, Diagnostics.Index_signature_in_type_0_only_permits_reading, typeToString(objectType));
Expand Down
8 changes: 4 additions & 4 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3761,7 +3761,7 @@ namespace ts {
/* @internal */
resolvedIndexType: IndexType;
/* @internal */
resolvedDeclaredIndexType: IndexType;
resolvedStringIndexType: IndexType;
/* @internal */
resolvedBaseConstraint: Type;
/* @internal */
Expand Down Expand Up @@ -3850,7 +3850,7 @@ namespace ts {
/* @internal */
resolvedIndexType?: IndexType;
/* @internal */
resolvedDeclaredIndexType?: IndexType;
resolvedStringIndexType?: IndexType;
}

// Type parameters (TypeFlags.TypeParameter)
Expand Down Expand Up @@ -3882,9 +3882,9 @@ namespace ts {

// keyof T types (TypeFlags.Index)
export interface IndexType extends InstantiableType {
/* @internal */
isDeclaredType?: boolean;
type: InstantiableType | UnionOrIntersectionType;
/* @internal */
stringsOnly: boolean;
}

export interface ConditionalRoot {
Expand Down