是否可以使 typescript-ref DTO 生成器尊重可为空的属性?
Is it possible to make typescript-ref DTO generator to respect nullable properties?
我正在尝试使用 ServiceStack 中的 typescript-ref
实用程序实现有效的 DTO 生成。问题是:对于可空属性和引用属性,它不会生成默认值定义。
有 DTO 的 C# 定义:
public class Data
{
public int Value { get; set; }
public int? OptionalValue { get; set; }
public string Text { get; set; }
}
生成的打字稿 DTO 将如下所示:
export class Data
{
public value: number;
public optionalValue: number;
public text: string;
public constructor(init?: Partial<Data>) { (Object as any).assign(this, init); }
}
这会导致静态检查问题。您将无法为这些属性设置 undefined
或 null
值(无论选择什么来表示 C# null
值)。由于 Partial
构造函数可以省略它们,但仍然不方便。
此外,TypeScript 编译器将不知道这些字段可能具有未定义的值 - 这就是我们将完全失去对这些 DTO 的静态检查的地方。
我发现 MakePropertiesOptional: True
documented option 将使生成的 DTO 中的每个 属性 成为可选的。但这并没有解决我的问题,而是导致了更多问题。有没有更灵活的方法解决?
我需要为上面的 class 生成 DTO 才能看起来像这样:
export class Data
{
public value: number;
public optionalValue?: number;
public text?: string;
public constructor(init?: Partial<Data>) { (Object as any).assign(this, init); }
}
我在最新的 ServiceStack v5.8.1 pre-relase now on MyGet.
中改进了对此的支持
默认实现现在应该为 Nullable 属性生成可选的 TypeScript 属性。所以默认情况下它现在会生成:
export class Data
{
public value: number;
public optionalValue?: number;
public text: string;
public constructor(init?: Partial<Data>) { (Object as any).assign(this, init); }
}
如果只需要特定的属性而所有其他属性都是可选的,您可以启用 MakePropertiesOptional: True
选项,然后使用 [Required]
属性标记需要哪些属性,例如:
public class Data
{
[Required]
public int Value { get; set; }
public int? OptionalValue { get; set; }
public string Text { get; set; }
}
这将生成您想要的:
export class Data
{
// @Required()
public value: number;
public optionalValue?: number;
public text?: string;
public constructor(init?: Partial<Data>) { (Object as any).assign(this, init); }
}
另一种使所有 reference 和 nullable 属性可选和所需值类型的选项是使用新的 IsPropertyOptional
过滤器,例如:
TypeScriptGenerator.IsPropertyOptional = (generator, type, prop) =>
prop.IsValueType != true || prop.Type == typeof(Nullable<>).Name;
或者使用新的 PropertyTypeFilter
,您可以改为让每个 属性 都可以为 null,例如:
TypeScriptGenerator.IsPropertyOptional = (generator, type, prop) => false;
TypeScriptGenerator.PropertyTypeFilter = (gen, type, prop) =>
gen.GetPropertyType(prop, out var isNullable) + "|null";
现在您设置的配置是:
TypeScriptGenerator.UseNullableProperties = true;
这将生成每个 属性 为空,例如:
export class Data
{
public value: number|null;
public optionalValue: number|null;
public text: string|null;
public constructor(init?: Partial<Data>) { (Object as any).assign(this, init); }
}
我正在尝试使用 ServiceStack 中的 typescript-ref
实用程序实现有效的 DTO 生成。问题是:对于可空属性和引用属性,它不会生成默认值定义。
有 DTO 的 C# 定义:
public class Data
{
public int Value { get; set; }
public int? OptionalValue { get; set; }
public string Text { get; set; }
}
生成的打字稿 DTO 将如下所示:
export class Data
{
public value: number;
public optionalValue: number;
public text: string;
public constructor(init?: Partial<Data>) { (Object as any).assign(this, init); }
}
这会导致静态检查问题。您将无法为这些属性设置 undefined
或 null
值(无论选择什么来表示 C# null
值)。由于 Partial
构造函数可以省略它们,但仍然不方便。
此外,TypeScript 编译器将不知道这些字段可能具有未定义的值 - 这就是我们将完全失去对这些 DTO 的静态检查的地方。
我发现 MakePropertiesOptional: True
documented option 将使生成的 DTO 中的每个 属性 成为可选的。但这并没有解决我的问题,而是导致了更多问题。有没有更灵活的方法解决?
我需要为上面的 class 生成 DTO 才能看起来像这样:
export class Data
{
public value: number;
public optionalValue?: number;
public text?: string;
public constructor(init?: Partial<Data>) { (Object as any).assign(this, init); }
}
我在最新的 ServiceStack v5.8.1 pre-relase now on MyGet.
中改进了对此的支持默认实现现在应该为 Nullable 属性生成可选的 TypeScript 属性。所以默认情况下它现在会生成:
export class Data
{
public value: number;
public optionalValue?: number;
public text: string;
public constructor(init?: Partial<Data>) { (Object as any).assign(this, init); }
}
如果只需要特定的属性而所有其他属性都是可选的,您可以启用 MakePropertiesOptional: True
选项,然后使用 [Required]
属性标记需要哪些属性,例如:
public class Data
{
[Required]
public int Value { get; set; }
public int? OptionalValue { get; set; }
public string Text { get; set; }
}
这将生成您想要的:
export class Data
{
// @Required()
public value: number;
public optionalValue?: number;
public text?: string;
public constructor(init?: Partial<Data>) { (Object as any).assign(this, init); }
}
另一种使所有 reference 和 nullable 属性可选和所需值类型的选项是使用新的 IsPropertyOptional
过滤器,例如:
TypeScriptGenerator.IsPropertyOptional = (generator, type, prop) =>
prop.IsValueType != true || prop.Type == typeof(Nullable<>).Name;
或者使用新的 PropertyTypeFilter
,您可以改为让每个 属性 都可以为 null,例如:
TypeScriptGenerator.IsPropertyOptional = (generator, type, prop) => false;
TypeScriptGenerator.PropertyTypeFilter = (gen, type, prop) =>
gen.GetPropertyType(prop, out var isNullable) + "|null";
现在您设置的配置是:
TypeScriptGenerator.UseNullableProperties = true;
这将生成每个 属性 为空,例如:
export class Data
{
public value: number|null;
public optionalValue: number|null;
public text: string|null;
public constructor(init?: Partial<Data>) { (Object as any).assign(this, init); }
}