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

Square brackets added to nullable string #331

Open
johnknoop opened this issue May 2, 2020 · 10 comments
Open

Square brackets added to nullable string #331

johnknoop opened this issue May 2, 2020 · 10 comments

Comments

@johnknoop
Copy link

I'm experiencing problems after declaring some strings as nullable, using the new nullable ref types in C# 8.

For any C# string?, the property.Type.Name will be string[].

Any idea how to get around this?

@grey280
Copy link

grey280 commented May 6, 2020

I've run into the same issue, but have yet to find a workaround.

@johnknoop
Copy link
Author

@grey280 My workaround for now is to disable Typewriter and edit the .ts files manually. But that's not sustainable in the long run of course.

@johnknoop
Copy link
Author

@frhagn is this something you think you'll fix eventually?

@SamuelKupferschmid
Copy link

For me this workaroud works:

if(p.Type.Name.EndsWith("[]") && !p.Type.FullName.StartsWith("System.Collections")) {
        return p.Type.Name.Substring(0, p.Type.Name.Length - 2);
 } else {
        return p.Type.Name;
 }

It would be nice if this project comes alive somehow.

@grey280
Copy link

grey280 commented Jul 16, 2020

Here's my variant of the workaround:

// special case: nullable value types (i.e. String?) register as Enumerable (Typewriter bug), need to be handled
bool hasNullableAttribute = p.Type.Attributes.Any(x => x.Name.ToLower() == "nullable");
bool endsWithBrackets = p.Type.Name.ToLower().EndsWith("[]");
bool isEnumerable = p.Type.FullName.StartsWith("System.Collections");
if (hasNullableAttribute && endsWithBrackets && !isEnumerable){
    return $"{p.Type.Name.Substring(0, p.Type.Name.Length - 2)} | null";
}
return $"{p.Type.Name} | null";

@johnknoop
Copy link
Author

@grey280 @SamuelKupferschmid Any ideas on how to make this approach work for this case?

public Dictionary<string, string?> Nodes { get; set; }

@grey280
Copy link

grey280 commented Jul 17, 2020

@johnknoop There'd be some very manual checks in that case, I suspect - I've been tinkering with trying to make something to recursively handle generics, but so far I keep running up against the disconnect between a Type and a TypeParameter.

@johnknoop
Copy link
Author

johnknoop commented Jul 17, 2020

@grey280 I got it working:

		string ProduceDictionaryType(Type type) {
			bool endsWithBrackets = type.TypeArguments.Last().Name.ToLower().EndsWith("[]");
			bool isEnumerable = type.TypeArguments.Last().FullName.StartsWith("System.Collections");
			bool isNullableString = endsWithBrackets && !isEnumerable;
			
			var suffix = isNullableString ? " | null | undefined" : "";
			
			var valueType = isNullableString ?
				$"{type.TypeArguments.Last().Name.Substring(0, type.TypeArguments.Last().Name.Length - 2)}"
				: type.TypeArguments.Last().Name;
			
			return $"{{ [key: {type.TypeArguments.First()}]: {valueType}{suffix}; }}";
		}

@johnknoop
Copy link
Author

Now the last thing I need in order to be able to start using TypeWriter again, is nullable reference types that aren't strings.

@AdaskoTheBeAsT
Copy link

fixed in unoffical release https://github.com/AdaskoTheBeAsT/Typewriter

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

No branches or pull requests

4 participants