Code Monkey home page Code Monkey logo

Comments (6)

NeVeSpl avatar NeVeSpl commented on May 27, 2024

There is no global configuration for custom functions available, but in NTypewriter (contrary to Typewriter) custom functions can have parameters, thus I would just add an additional optional parameter to this method to affect its behaviour.

You are welcome to prepare a pull request, just do not forget to include tests for this change.

from ntypewriter.

RudeySH avatar RudeySH commented on May 27, 2024

I would like to add a function similar to ToTypeScriptType that does not append anything for nullables. Such a function would be much more flexible, you can now easily append a postfix yourself, like so:

{{ if property.Type.IsNullable }} | undefined{{ end }}

Full example:

{{ property.Name | String.ToCamelCase }}: {{ property.Type | Type.ToTypeScriptType }}{{ if property.Type.IsNullable }} | undefined{{ end }};

This would cover all cases, but requires more code in the template (not a bad thing in my opinion).

What are your thoughts on this? If you agree, what should this function be named so that the distinction between ToTypeScriptType and the new function is obvious? Ideally the behavior of ToTypeScriptType could be changed to avoid polluting the interface, but that would be a breaking change.

from ntypewriter.

NeVeSpl avatar NeVeSpl commented on May 27, 2024

At the current stage, breaking changes are not a problem. But in that case, I would leave the behaviour of ToTypeScriptType as it is, because | null is the most accurate equivalent of nullable type.

The function that you described already exists, only needs a few tweaks: add null checking, documentation, make it public

private static string ToTypeScriptTypePhase2(IType type)

But the name is awful and it shows that I did not have an idea how to name it. Something better is needed, that will underline distinction. And most obvious choices seem too long :
ToTypeScriptTypeNotNullable
ToTypeScriptTypeRaw
ToTypeScriptTypeExtant
and more ideas on how to replace the phrase not null:
https://stackoverflow.com/questions/14273820/is-there-a-single-word-meaning-not-null

from ntypewriter.

RudeySH avatar RudeySH commented on May 27, 2024

I know you just said you want to leave the behavior of ToTypeScriptType as-is, but I'll make the following suggestions anyway:

How about renaming the existing function to ToTypeScriptUnionType and use ToTypeScriptType for the non-nullable variant?

Or, perhaps even better, an optional boolean argument named "nullable". It could be true by default if that's the default behavior you'd want.

from ntypewriter.

NeVeSpl avatar NeVeSpl commented on May 27, 2024

I would go with the proposed nullable parameter, it seems most obvious from a user perspective what it does.

from ntypewriter.

RudeySH avatar RudeySH commented on May 27, 2024

After doing some work on this, I realized that having only an option to exclude the nullable postfix isn't going to be sufficient for my use case. Arrays and generics make things a little more complicated. The function currently produces the following:

myProperty: int | null[]
myProperty: MyGeneric<int | null>

In these cases, I'd like to be able to configure NTypewriter to use | undefined instead.

By the way, int | null[] is a bug. This is a type that can either be an int, or an array containing nulls. This does not represent an array that contains nullable ints.. It should be (int | null)[] or Array<int | null> instead. I will create a pull request soon that fixes this problem.

from ntypewriter.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.