Skip to content

add type guard of constructor#23622

Closed
Kingwl wants to merge 5 commits intomicrosoft:masterfrom
Kingwl:constructor-narrow
Closed

add type guard of constructor#23622
Kingwl wants to merge 5 commits intomicrosoft:masterfrom
Kingwl:constructor-narrow

Conversation

@Kingwl
Copy link
Copy Markdown
Contributor

@Kingwl Kingwl commented Apr 23, 2018

Fixes #23274

@Kingwl
Copy link
Copy Markdown
Contributor Author

Kingwl commented Apr 23, 2018

what should i do with primitives ?

@DanielRosenwasser
Copy link
Copy Markdown
Member

DanielRosenwasser commented Apr 24, 2018

what should i do with primitives ?

I'd filter down to the primitive types (e.g. number, string, true, false`, the other literal/unit types, etc.) whose apparent type is constructed by the checked constructor.

So

declare let x: number | true | "hello" | "world;

if (x.constructor === String) {
  // x has type `"hello" | "world"` here.
}

Comment thread src/compiler/checker.ts Outdated
const targetType = !isTypeAny(prototypePropertyType) ? prototypePropertyType : undefined;
if (!targetType || isTypeAny(type) && (targetType === globalObjectType || targetType === globalFunctionType)) return type;

return getNarrowedType(type, targetType, assumeTrue, areTypesComparable);
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.

I don't think you want areTypesComparable because this will work for types which don't explicitly derive from a given constructor.

I think you potentially want a helper function that uses isTypeDerivedFrom on the wrapper object type for any primitive types.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

will work for types which don't explicitly derive from a given constructor

could you give some example? 😢

Comment thread src/compiler/utilities.ts Outdated
}

/** @internal */
export function isExclamationEqualsOrExclamationEqualsEqualsKind(kind: SyntaxKind): boolean {
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.

Maybe isNegatedEqualityToken? *shrug*

@Kingwl
Copy link
Copy Markdown
Contributor Author

Kingwl commented Jun 4, 2018

need take another look😢

@Kingwl
Copy link
Copy Markdown
Contributor Author

Kingwl commented Jun 14, 2018

🆙

@Kingwl Kingwl force-pushed the constructor-narrow branch from 77c3f73 to 49b364d Compare June 14, 2018 16:45
@Kingwl
Copy link
Copy Markdown
Contributor Author

Kingwl commented Jun 14, 2018

#24966

@Kingwl
Copy link
Copy Markdown
Contributor Author

Kingwl commented Jun 29, 2018

🆙

@Kingwl Kingwl force-pushed the constructor-narrow branch from 9f43125 to cd7dd47 Compare August 9, 2018 10:07
@RyanCavanaugh
Copy link
Copy Markdown
Member

Cycling to get a fresh CI

@RyanCavanaugh
Copy link
Copy Markdown
Member

@Kingwl closing for housekeeping; would welcome a fresh PR

@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.

Feature Request / Proposal: constructor type guard

3 participants