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

Optional type #22

Closed
dodyg opened this issue May 27, 2015 · 4 comments
Closed

Optional type #22

dodyg opened this issue May 27, 2015 · 4 comments

Comments

@dodyg
Copy link

dodyg commented May 27, 2015

Is there anyway to generate typescript definition with optional type? This is especially important when trying to generate typescript interfaces automatically

@frhagn
Copy link
Owner

frhagn commented Jun 2, 2015

Hi @dodyg, could you please provide some more details about this? An example would be nice.

@dodyg
Copy link
Author

dodyg commented Jun 2, 2015

How can I generate

interface Person {
  Name? : string
  Age? : string
}

@frhagn
Copy link
Owner

frhagn commented Jun 8, 2015

If you want it on all properties, just add the question mark in the template like this:

$Classes(*Models*)[
interface $Name { $Properties[
    $Name?: $Type;]
}]

Otherwise the problem is how to determine which properties should be decorated. One solution is to use attributes to mark optional properties in your c# classes e.g:

${
    var IsOptional = (Property p) => p.Attributes.Any(a => a.Name == "Optional");
}
$Classes(*Models*)[
interface $Name { $Properties[
    $Name$IsOptional[?]: $Type;]
}]
public class Person
{
    [Optional]
    public string Name { get; set; }
    public int Age { get; set; }
}

@dodyg
Copy link
Author

dodyg commented Jun 8, 2015

This is exactly what I need. Thank you so much. I just created a dummy Optional attribute and tag the necessary properties with it

[AttributeUsage(AttributeTargets.All)]
public class OptionalAttribute : Attribute
{
}

@dodyg dodyg closed this as completed Jun 8, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants