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 type guard of constructor #23622

Closed
wants to merge 5 commits into from
Closed

Conversation

Kingwl
Copy link
Contributor

@Kingwl Kingwl commented Apr 23, 2018

Fixes #23274

@Kingwl
Copy link
Contributor Author

Kingwl commented Apr 23, 2018

what should i do with primitives ?

@DanielRosenwasser
Copy link
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.
}

const targetType = !isTypeAny(prototypePropertyType) ? prototypePropertyType : undefined;
if (!targetType || isTypeAny(type) && (targetType === globalObjectType || targetType === globalFunctionType)) return type;

return getNarrowedType(type, targetType, assumeTrue, areTypesComparable);
Copy link
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
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? 😢

@@ -6335,4 +6335,9 @@ namespace ts {
export function isNamedImportsOrExports(node: Node): node is NamedImportsOrExports {
return node.kind === SyntaxKind.NamedImports || node.kind === SyntaxKind.NamedExports;
}

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

Kingwl commented Jun 4, 2018

need take another look😢

@Kingwl
Copy link
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
Contributor Author

Kingwl commented Jun 14, 2018

#24966

@Kingwl
Copy link
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
Member

Cycling to get a fresh CI

@RyanCavanaugh
Copy link
Member

@Kingwl closing for housekeeping; would welcome a fresh PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature Request / Proposal: constructor type guard
3 participants