-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Conversation
what should i do with |
I'd filter down to the primitive types (e.g. So declare let x: number | true | "hello" | "world;
if (x.constructor === String) {
// x has type `"hello" | "world"` here.
} |
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); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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? 😢
src/compiler/utilities.ts
Outdated
@@ -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 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe isNegatedEqualityToken
? *shrug*
need take another look😢 |
🆙 |
77c3f73
to
49b364d
Compare
🆙 |
9f43125
to
cd7dd47
Compare
Cycling to get a fresh CI |
@Kingwl closing for housekeeping; would welcome a fresh PR |
Fixes #23274