-
Notifications
You must be signed in to change notification settings - Fork 12.8k
strictNullChecks with optional properties on class change type #38323
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
Comments
The way you've written Keep in mind that types are structural and type unioning is commutative and associative, so whenever you have
// returns "chicken noodles", of course
declare var c: SomeClass;
getRefProperty<string | number | undefined, "chicken noodles">(c.prop); |
@nmain take a look at this example: class ObjectId {
anObjectId!: boolean;
}
export declare type RefType = number | string | ObjectId | Buffer;
type Ref<
R,
T extends RefType = R extends { _id: RefType } ? R["_id"] : ObjectId
> = R | T;
declare function isRefType<T, S extends RefType>(
doc: Ref<T, S>,
): doc is S;
class Model {
_id!: string; // change to ? and error below disappears
name!: string;
}
class Parent {
child!: Ref<Model>;
}
const x = new Parent();
isRefType(x.child) ? x.child.anObjectId : x.child._id; Here's a Playground Link Depending on whether I assume there's a type error somewhere in this code, but the issue originated from a similar example. |
Thinking about it, it seems that if a property is defined as nullable, then the
|
@nmain then why does it work (as expected by me) when
when -> the whole intention about |
If the argument's type is |
@nmain yes, but shouldnt when |
I don't see anything indicative of a bug here. Please file a new issue with a more explicit explanation of what's wrong if you'd like us to come back to it - "This isn't what I expected" is not really indicative of a defect without more context. |
@RyanCavanaugh here's an example where behavior is different with Just toggle The curious part is that simply changing the type on the first line to add
Resolves the issue, but it's unclear why. So I don't think there's a typescript bug here, just non-intuitive behavior. |
Nevermind, I understand the problem. More context here if someone runs into this in the future and needs help: typegoose/typegoose#254 (comment) :) Issue can probably be closed. |
thanks @andreialecu for explaining why this is happening, but i would have expected an error from typescript of "incompatible types" or something like that |
This issue has been marked as 'Question' and has seen no recent activity. It has been automatically closed for house-keeping purposes. If you're still waiting on a response, questions are usually better suited to stackoverflow. |
TypeScript Version: 4.0.0-dev.20200504
Search Terms: optional class strictNullChecks generics type
Code
Expected behavior:
that
prop
is of typetest<string>
(or in strict modetest<string> | undefined
)Actual behavior:
prop
is of typestring | number | undefined
Playground Link:
not working as expected: Playground Link
working as expected: Playground Link
Related Issues:
the current behavior breaks functions that depend on destructuring types
The text was updated successfully, but these errors were encountered: