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

Allow extending from any #14935

Merged
merged 12 commits into from
Apr 6, 2017
Merged

Allow extending from any #14935

merged 12 commits into from
Apr 6, 2017

Conversation

sandersn
Copy link
Member

@sandersn sandersn commented Mar 30, 2017

Extending from any adds an index signature: [s: string]: any to both the
instance and static sides of the class.

Fixes #14301
Fixes #15040

Extending from any adds an index signature: [s: string]: any to both the
instance and static sides of the class.
Also improve how the string indexer for any-inheriting types is added.
}

let c = new C();
c.known.length; // error, 'real' has no 'length' property

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

real / sreal

@sandersn sandersn assigned yuit and ghost Apr 5, 2017
@sandersn
Copy link
Member Author

sandersn commented Apr 5, 2017

@yuit @andy-ms mind taking a look?

@@ -4617,7 +4617,7 @@ namespace ts {
error(type.symbol.valueDeclaration, Diagnostics._0_is_referenced_directly_or_indirectly_in_its_own_base_expression, symbolToString(type.symbol));
return type.resolvedBaseConstructorType = unknownType;
}
if (baseConstructorType !== unknownType && baseConstructorType !== nullWideningType && !isConstructorType(baseConstructorType)) {
if (baseConstructorType !== anyType && baseConstructorType !== unknownType && baseConstructorType !== nullWideningType && !isConstructorType(baseConstructorType)) {
Copy link
Member

@DanielRosenwasser DanielRosenwasser Apr 5, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hard for me to look up right now whether unknownType includes it, could you check for TypeFlags.Any?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just check unknownType and anyType both have TypeFlags.Any

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Includes anyType, autoType, unknownType. Seems like it should work.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

// object types.
function isValidBaseType(type: Type): boolean {
return type.flags & (TypeFlags.Object | TypeFlags.NonPrimitive) && !isGenericMappedType(type) ||
return !!(type.flags & TypeFlags.Any) ||
type.flags & (TypeFlags.Object | TypeFlags.NonPrimitive) && !isGenericMappedType(type) ||
Copy link
Contributor

@yuit yuit Apr 5, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this just be type.flags & (TypeFlags.Object | TypeFlags.NonPrimitive | TypeFlags.Any)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should work, although it's kind of a weird pun in that we rely on !isGenericMappedType(anyType) being true.

@@ -5130,7 +5134,9 @@ namespace ts {
addInheritedMembers(members, getPropertiesOfType(instantiatedBaseType));
callSignatures = concatenate(callSignatures, getSignaturesOfType(instantiatedBaseType, SignatureKind.Call));
constructSignatures = concatenate(constructSignatures, getSignaturesOfType(instantiatedBaseType, SignatureKind.Construct));
stringIndexInfo = stringIndexInfo || getIndexInfoOfType(instantiatedBaseType, IndexKind.String);
stringIndexInfo = stringIndexInfo || (instantiatedBaseType === anyType ?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: would it be easier to read this way:

if (!stringIndexInfo) {
stringIndexInfo = instantiatedBaseType === anyType ? createIndexInfo(anyType, /*isReadonly*/ false) : getIndexInfoOfType(instantiatedBaseType, IndexKind.String);
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess so. Done.

@ghost
Copy link

ghost commented Apr 5, 2017

I wonder if anything from #12352 could be simplified now.
At least the comment about getBaseConstructorTypeOfClass could be updated.

@@ -4662,6 +4662,9 @@ namespace ts {
// type arguments in the same manner as a type reference to get the same error reporting experience.
baseType = getTypeFromClassOrInterfaceReference(baseTypeNode, baseConstructorType.symbol);
}
else if (baseConstructorType.flags & TypeFlags.Any) {
baseType = anyType;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you leave a comment saying why it is necessary to set it to anyType instead of baseConstructorType?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no good reason; in fact it reduces test churn to return baseConstructorType for tests that extend undefined variables, which I think get unknownType instead of anyType, meaning that you get slightly more errors.

I'll change it to baseConstructorType.

@sandersn
Copy link
Member Author

sandersn commented Apr 5, 2017

@andy-ms Maybe? I don't understand the differences between using unknownSymbol (which I guess has unknownType) for a missing module vs creating a synthetic symbol. Did the synthetic symbol get created solely for error reporting?

Seems like it would be worth reverting #12352 to see how it behaves, because I'm not able to predict much about module behaviour a priori.

@ghost
Copy link

ghost commented Apr 5, 2017

OK, we can try reverting #12352 later.
But please update this comment:

// This must be different than unknownSymbol because getBaseConstructorTypeOfClass won't fail for unknownSymbol.

Extending symbols from untyped modules is no longer an error, so #12532
didn't get us anything except slightly better quick info.
@sandersn
Copy link
Member Author

sandersn commented Apr 5, 2017

Reverting #12352 turned out to be straightforward, so I ended up merging that branch back into this PR.

@@ -1333,7 +1331,7 @@ namespace ts {
if (targetSymbol) {
const name = specifier.propertyName || specifier.name;
if (name.text) {
if (isUntypedOrShorthandAmbientModuleSymbol(moduleSymbol)) {
if (isShorthandAmbientModuleSymbol(moduleSymbol)) {
Copy link

@ghost ghost Apr 5, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the old name better, even if it's a bit long.

Test asserts that unused locals error works for untyped modules.
Comment no longer claims to check for untyped modules.
@sandersn sandersn merged commit 3029b8f into master Apr 6, 2017
@sandersn sandersn deleted the allow-extending-from-any branch April 6, 2017 16:18
@microsoft microsoft locked and limited conversation to collaborators Jun 21, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants