Comments (6)
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.
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.
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
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.
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.
I would go with the proposed nullable
parameter, it seems most obvious from a user perspective what it does.
from ntypewriter.
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)
- Changes from GIT not picked up by NTypewriter HOT 3
- This is perfect for AI
- Failed to compile user code (System.Collections) HOT 10
- Source Generator ArgumentNullException HOT 8
- Save function is misleading HOT 3
- `Microsoft.CodeAnalysis` not found HOT 10
- SourceGenerator can't find dependent projects HOT 2
- Publish latest releases as NuGet package HOT 4
- Template output preview is always displayed on top of everything HOT 1
- Template output preview with dark mode support
- Using C# 9.0 init accessors in a custom function gives an error inside Visual Studio extension HOT 1
- Extend AllReferencedTypes to search on Interfaces HOT 1
- Request - Property internal set vs public flag or filter HOT 3
- AttributeArgument.Value.Tostring() causes exception: TypedConstant is an array. Use Values property
- Unable to build project with SourceGenerator: 'ITypedConstant' could not be found HOT 2
- Unable to debug SourceGenerator, no output and no log files created HOT 6
- Why does Type.ToTypeScriptType ignore nullable if [Required] attribute is present? HOT 4
- Template processing fails when reading arguments from an Attribute that has arguments with default values (Roslyn project) HOT 3
- Missing default value for DateTime
- Incremental Generator HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ntypewriter.