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

Avoid generating implementation signatures in ambient contexts.#19708

Closed
DanielRosenwasser wants to merge 4 commits into
mastermicrosoft/TypeScript:masterfrom
quickFixImplIntAmbientContextsmicrosoft/TypeScript:quickFixImplIntAmbientContextsCopy head branch name to clipboard
Closed

Avoid generating implementation signatures in ambient contexts.#19708
DanielRosenwasser wants to merge 4 commits into
mastermicrosoft/TypeScript:masterfrom
quickFixImplIntAmbientContextsmicrosoft/TypeScript:quickFixImplIntAmbientContextsCopy head branch name to clipboard

Conversation

@DanielRosenwasser

Copy link
Copy Markdown
Member

Fixes #19703

@DanielRosenwasser DanielRosenwasser requested review from a user and aozgaa November 3, 2017 07:34

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

It might be better to just add declare interface C extends I {} above the class do avoid duplicating methods.

Comment thread src/services/codefixes/helpers.ts Outdated
// (eg: an abstract method or interface declaration), there is a 1-1
// correspondence of declarations and signatures.
const signatures = checker.getSignaturesOfType(type, SignatureKind.Call);
const needsImplementation = !isInAmbientContext(enclosingDeclaration);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Use NodeFlags.Ambient (#17831)

}
}
else {
else if (needsImplementation) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Is it possible that declarations.length > signatures.length but we don't want to add an implementation?
While I'm here, TypeChecker.signatureToSignatureDeclaration has a non-nullable result. Then in signatureToMethodDeclaration it's cast to another non-nullable type. Then it's tested for existence!

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.

Is it possible that declarations.length > signatures.length but we don't want to add an implementation?

Good catch, but I don't even understand what that check is for and what it's doing. @aozgaa?

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.

See this comment (a few lines above): https://github.com/Microsoft/TypeScript/pull/19708/files#diff-6d6034083b71c2ed75493db0fe725ec5R88.

An example (haven't checked by actual debugging) might be

class C {
     foo(x: number): void;
     foo(x: string): void;
     foo(x: number | string) { console.log(x); }
}

declare class D implements C {
}

Then I think adding foo to D would be an example.

@aozgaa aozgaa Nov 17, 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.

I think this check should be inside createMethodImplementingSignatures, which will just omit the body if we are in an ambient context.

EDIT: no, we do not need an implementation signature in this case. Doh.

modifiers,
/*asteriskToken*/ undefined,
name,
optional ? createToken(SyntaxKind.QuestionToken) : undefined,

@ghost ghost Nov 3, 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.

It's wierd to create an optional method with a body even if that's legal.
If the method is optional, though, we don't want to make it look like it must be implemented; maybe just add an // optional, delete if you don't want to implement this comment and let the user decide?

parameters,
returnType,
createStubbedMethodBody());
/*returnType*/ undefined,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

We might as well give them the return type from the signature, right?

@DanielRosenwasser DanielRosenwasser Nov 3, 2017

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.

Technically it's unsound to union the return types in most cases, but we could do that in a different PR.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Intersection instead? It's better that they get an error when trying to return a value from the method, than that they get an error that the class failed to implement the interface, which are always hard to read..

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.

The intersection will be impossible to fulfill most of the time.

function foo(x: number): number;
function foo(x: string): string;
function foo(x: string | number): ??? {
  // An intersection is impossible to return
}

All I'm saying is that it's unsound to do the union, but we do allow it because it's just convenient. You're right that we should probably still do it. Just felt weird.

@DanielRosenwasser DanielRosenwasser force-pushed the quickFixImplIntAmbientContexts branch from aeeeee5 to 8a211ae Compare November 3, 2017 22:11
@mhegazy

mhegazy commented Nov 9, 2017

Copy link
Copy Markdown
Contributor

@DanielRosenwasser can you please refresh this PR and address the test failures..

@mhegazy

mhegazy commented Mar 29, 2018

Copy link
Copy Markdown
Contributor

@DanielRosenwasser can we get these in?

@typescript-bot

Copy link
Copy Markdown
Contributor

Thanks for your contribution. This PR has not been updated in a while and cannot be automatically merged at the time being. For housekeeping purposes we are closing stale PRs. If you'd still like to continue working on this PR, please leave a message and one of the maintainers can reopen it.

@RyanCavanaugh RyanCavanaugh deleted the quickFixImplIntAmbientContexts branch June 16, 2022 22:06
@microsoft microsoft locked as resolved and limited conversation to collaborators Oct 21, 2025
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.

4 participants

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