我最近一直在使用 TypeScript,偶然发现了一个让我感到困惑的问题。我已将问题放入一个示例中,如下所示:
type Options<T> = {
props: T;
g: (x: T) => void;
};
function f<T>(options: Options<T>) {
console.log(options);
}
// Example 1 (x and props expected type)
f({
props: {
foo: {
a: 200,
bar: () => {}
}
},
g(x) {
// x is the expected type
console.log(x)
}
});
// Example 2 (x and props unknown)
f({
props: {
foo: {
a: 100,
bar: function() {}
}
},
g(x) {
// x is unknown
console.log(x)
}
}); …Run Code Online (Sandbox Code Playgroud) 我在我的 Web-API 项目(.NET Core 3.1)中使用 Dapper,并尝试将记录插入数据库(SQL Server 2017)+返回创建的 id。
我有这个对象...
public class CreateDetailGroupDto
{
public string Name { get; set; }
public List<CreateDetailDto> Details { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
负责的方法如下所示:
public async Task<int> Create(CreateDetailGroupDto detailGroup)
{
using var cnn = Connection;
var parameter = new DynamicParameters();
parameter.Add("detailGroup", detailGroup);
parameter.Add("id", dbType: DbType.Int32, direction: ParameterDirection.Output);
await cnn.ExecuteAsync("spDetailGroup_Insert", param: parameter, commandType: CommandType.StoredProcedure);
int id = parameter.Get<int>("id");
return id;
}
Run Code Online (Sandbox Code Playgroud)
我正在执行存储过程,spDetailGroup_Insert该过程需要 JSON 格式的上述对象NVARCHAR(MAX),并将 id 作为输出参数。
我编写了一个处理程序,该处理程序应该在用作参数时序列化对象(我认为):
public class TypeHandler<T> : …Run Code Online (Sandbox Code Playgroud) 在 TypeScript 中,有没有一种方法可以根据某些元组获取嵌套属性的类型?给定以下示例,假设元组是["bs", 0, "c"],那么类型应该是boolean(或者["bs", 0, "ds", 0, "f"]等等number)。
interface Foo {
a: string;
bs: {
c: boolean;
ds: {
e: null;
f: number;
}[];
}[];
}
Run Code Online (Sandbox Code Playgroud)
对于某些上下文,我想输入一个带有两个参数 apath和 a 的函数value。对于某些对象,如果给定路径的值是一个数组,它将是push参数value。这个函数的实现可以在这个 Playround中找到。我已经在寻找一些解决方案,例如在本期中,但我认为我的问题有点不同。