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

Add 'isNamedDeclaration' helper to reduce casts #22089

Merged
3 commits merged into from
Mar 8, 2018
Merged

Conversation

ghost
Copy link

@ghost ghost commented Feb 21, 2018

Especially helps with #22088 because testing (node as NamedDeclaration).name doesn't make (node as NamedDeclaration).name defined in future uses.

@@ -4298,6 +4298,11 @@ namespace ts {
return declaration.name || nameForNamelessJSDocTypedef(declaration);
}

/** @internal */
export function isNamedDeclaration(node: Node): node is NamedDeclaration & { name: DeclarationName } {
Copy link
Member

Choose a reason for hiding this comment

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

Mmm, this implementation doesn't seem to actually check that the name is a DeclarationName.

Copy link
Author

Choose a reason for hiding this comment

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

Added an assertion -- a name property should always be a DeclarationName.

@ghost ghost force-pushed the isNamedDeclaration branch from 1299534 to b1c15f4 Compare February 22, 2018 16:07
@ghost ghost force-pushed the isNamedDeclaration branch from b1c15f4 to 52d830f Compare February 22, 2018 16:08
/** @internal */
export function isNamedDeclaration(node: Node): node is NamedDeclaration & { name: DeclarationName } {
const name = (node as NamedDeclaration).name;
Debug.assert(!name || isDeclaration(node) && node.name === name, "A 'name' property should always be a DeclarationName.");
Copy link
Contributor

Choose a reason for hiding this comment

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

this really a hot path, i would rather not do additional work. this is rather simple check. does it have a name property or not.

@ghost ghost force-pushed the isNamedDeclaration branch from 96b9248 to 142824b Compare March 5, 2018 21:41
@ghost ghost merged commit 2676786 into master Mar 8, 2018
@ghost ghost deleted the isNamedDeclaration branch March 8, 2018 15:50
@cowchimp
Copy link

cowchimp commented May 5, 2018

Hi. is this new Type Guard not exposed externally as all of the other Type Guards?
I'm using Typescript 2.8.3 and it's not listed inside of lib/typescript.d.ts so I get a compiler error when I try to use it in my code.
Thanks.

@mhegazy
Copy link
Contributor

mhegazy commented May 5, 2018

You can send us a PR to remove the /** @internal */ comment

@microsoft microsoft locked and limited conversation to collaborators Jul 31, 2018
This pull request was closed.
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.

3 participants