Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Allow extending from any#14935

Merged
sandersn merged 12 commits into
mastermicrosoft/TypeScript:masterfrom
allow-extending-from-anymicrosoft/TypeScript:allow-extending-from-anyCopy head branch name to clipboard
Apr 6, 2017
Merged

Allow extending from any#14935
sandersn merged 12 commits into
mastermicrosoft/TypeScript:masterfrom
allow-extending-from-anymicrosoft/TypeScript:allow-extending-from-anyCopy head branch name to clipboard

Conversation

@sandersn

@sandersn sandersn commented Mar 30, 2017

Copy link
Copy Markdown
Member

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

Copy link
Copy Markdown

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

sandersn commented Apr 5, 2017

Copy link
Copy Markdown
Member Author

@yuit @Andy-MS mind taking a look?

Comment thread src/compiler/checker.ts Outdated
return type.resolvedBaseConstructorType = unknownType;
}
if (baseConstructorType !== unknownType && baseConstructorType !== nullWideningType && !isConstructorType(baseConstructorType)) {
if (baseConstructorType !== anyType && baseConstructorType !== unknownType && baseConstructorType !== nullWideningType && !isConstructorType(baseConstructorType)) {

@DanielRosenwasser DanielRosenwasser Apr 5, 2017

Copy link
Copy Markdown
Member

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

done

Comment thread src/compiler/checker.ts Outdated
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) ||

@yuit yuit Apr 5, 2017

Copy link
Copy Markdown
Contributor

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
Copy Markdown
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.

Comment thread src/compiler/checker.ts Outdated
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
Copy Markdown
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
Copy Markdown
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

ghost commented Apr 5, 2017

Copy link
Copy Markdown

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

Comment thread src/compiler/checker.ts Outdated
baseType = getTypeFromClassOrInterfaceReference(baseTypeNode, baseConstructorType.symbol);
}
else if (baseConstructorType.flags & TypeFlags.Any) {
baseType = anyType;

Copy link
Copy Markdown

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
Copy Markdown
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

sandersn commented Apr 5, 2017

Copy link
Copy Markdown
Member Author

@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

ghost commented Apr 5, 2017

Copy link
Copy Markdown

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

sandersn commented Apr 5, 2017

Copy link
Copy Markdown
Member Author

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

Comment thread src/compiler/checker.ts
const name = specifier.propertyName || specifier.name;
if (name.text) {
if (isUntypedOrShorthandAmbientModuleSymbol(moduleSymbol)) {
if (isShorthandAmbientModuleSymbol(moduleSymbol)) {

@ghost ghost Apr 5, 2017

Copy link
Copy Markdown

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

Morty Proxy This is a proxified and sanitized view of the page, visit original site.