Skip to content

Add Option type for NULL fields #263

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

Merged
merged 5 commits into from
Jul 28, 2020

Conversation

meme
Copy link
Contributor

@meme meme commented Jul 20, 2020

Copy link
Contributor

@zengin zengin left a comment

Choose a reason for hiding this comment

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

We should check if the property is nullable or not.

We should also consider the implications of wrapping the types with Option to dot notation usage in IDEs.

@meme
Copy link
Contributor Author

meme commented Jul 20, 2020

How can I check if a property is null-able?

@zengin
Copy link
Contributor

zengin commented Jul 20, 2020

prop.IsNullable

@meme
Copy link
Contributor Author

meme commented Jul 20, 2020

Tried my best to follow the styling~ let me know if any changes need to be made with my indentation.

@meme
Copy link
Contributor Author

meme commented Jul 27, 2020

Any movement on this?

@MIchaelMainer
Copy link
Contributor

MIchaelMainer commented Jul 27, 2020

  • Add the same support for complexTypes. This is only added for entityTypes.

This adds type Option<T> = T | undefined | null; which then makes these changes:

image

This appears to be a breaking change as the following code results in the following message: Argument of type '{ id: string; displayName: string; }' is not assignable to parameter of type 'UserIdentity'. Type '{ id: string; displayName: string; }' is missing the following properties from type 'UserIdentity': ipAddress, userPrincipalName

export interface UserIdentity {
    // Unique identifier for the identity.
    id: Option<string>;
    // The identity's display name. Note that this may not always be available or up-to-date.
    displayName: Option<string>;
    // Indicates the client IP address used by user performing the activity (audit log only).
    ipAddress: Option<string>;
    // The userPrincipalName attribute of the user.
    userPrincipalName: Option<string>;
}

/* Same issue */
// export interface UserIdentity {
//     // Unique identifier for the identity.
//     id: string | null | undefined;
//     // The identity's display name. Note that this may not always be available or up-to-date.
//     displayName: string | null | undefined;
//     // Indicates the client IP address used by user performing the activity (audit log only).
//     ipAddress: string | null | undefined;
//     // The userPrincipalName attribute of the user.
//     userPrincipalName: string | null | undefined;
// }

/* Original - this shows no warnings from TS */
// export interface UserIdentity {
//     // Unique identifier for the identity.
//     id?: string;
//     // The identity's display name. Note that this may not always be available or up-to-date.
//     displayName?: string;
//     // Indicates the client IP address used by user performing the activity (audit log only).
//     ipAddress?: string;
//     // The userPrincipalName attribute of the user.
//     userPrincipalName?: string;
// }

type Option<T> = T | undefined | null;

function printId(userIdentity: UserIdentity) {
    console.log(userIdentity.id);
}

// let myObj = { id: "10", displayName: "Size 10 Object", ipAddress: null, userPrincipalName: null };
let myObj = { id: "10", displayName: "Size 10 Object" };

printId(myObj);

@meme Can you help me understand what I see here?

@meme
Copy link
Contributor Author

meme commented Jul 27, 2020

This was an oversight~ since undefined is embedded in the type, it does not act the same as an optional field with, e.g.: name?: string. I have corrected this issue in my latest commit, so your example should now work correctly.

@ghost
Copy link

ghost commented Jul 27, 2020

CLA assistant check
All CLA requirements met.

@zengin
Copy link
Contributor

zengin commented Jul 27, 2020

To cover the point I mentioned in my review, dot notation in VSCode auto-complete doesn't break with this change.

@MIchaelMainer MIchaelMainer merged commit d4e0fd7 into microsoftgraph:dev Jul 28, 2020
@MIchaelMainer
Copy link
Contributor

closes #257

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.

3 participants