gpt4 book ai didi

Can context sensitive inference be "pushed up" chained method invocations?(上下文敏感推理是否可以“向上推入”链式方法调用?)

翻译 作者:bug小助手 更新时间:2023-10-26 22:39:14 25 4
gpt4 key购买 nike



I'm trying to build a Zod-ish API, but with type inference of values instead of the z.string()-is-definitely a string, i.e. I want to do something like:

我正在尝试构建一个Zod-ish API,但是使用值的类型推断而不是z.string()--绝对是一个字符串,也就是说,我想做一些类似的事情:


// Create a Zod-style fluent DSL for building config
const c = {
value<V>(): ValueConfig<V> {
return null!;
},
};

type Config<T> = { [P in keyof T]: ValueConfig<T[P]> };

function config<T>(object: Config<T>): void {}

// Allow fluent methods
interface ValueConfig<V> {
req(): ValueConfig<V>;
readOnly(): ValueConfig<V>;
validate(fn: (value: V) => boolean): ValueConfig<V>;
}

// A pojo
type Author = { name: string; address: string; city: string };

// Try and configure the author
config<Author>({
// correctly inferred as c.value<string> --> ValueConfig<string>
name: c.value(),
// inferred only as c.value<unknown> --> ValueConfig<unknown>
address: c.value().req(),
// compile error, v is implicitly typed as any
city: c.value().validate((v) => v.length > 0),
});

The issue is that context-sensitive inference works for "just one method call", i.e. name: c.value() is inferred as ValueConfig<string>, but once I try chaining method calls, i.e. address: c.value().req() or city: c.value().validate(...), the string-ness doesn't get pushed up into the originating c.value() to then flow into the .req().

问题是上下文相关的推理只适用于“一个方法调用”,即名称:c.Value()被推断为ValueConfig.req(),但一旦我尝试链接方法调用,即Address:c.Value().req()或City:c.Value().Valid(...),字符串不会被推入原始的c.Value(),然后流入.req()。


I assume this is just a limitation of context sensitive inference? Are there any alternative approaches to achieving a similar API?

我想这只是上下文相关推理的一个限制?有没有其他方法可以实现类似的API?


更多回答

This is almost certainly a limitation of context sensitive inference; maybe ms/TS#47599 is the most appropriate github issue about it, but there will always be limitations like this. As for alternative approaches I think I'd need to see more of a minimal reproducible example of the functionality needed (unless you think this question needs knowledge of zod to answer, in which case you should tag it as such). Right now the types are so general that you can move generics around like this and it "works". How do you want to proceed?

这几乎肯定是上下文敏感推理的一个限制;也许ms/TS#47599是关于它的最合适的github问题,但总是会有这样的限制。至于替代方法,我认为我需要看到更多的所需功能的最小可重复示例(除非你认为这个问题需要zod的知识来回答,在这种情况下,你应该这样标记它)。现在的类型是如此通用,你可以像这样移动泛型,它“工作”。你想怎么做?

Hi @jcalz! I had seen 47599 and that makes sense, maybe I'll add my use case there... per an example, my library's API currently requires typing out ADTs / object literals like { type: "value" | "list" | ... }, and imo Zod-style fluent APIs have become popular, so I'm trying to mimic that. If you're curious: github.com/homebound-team/form-state/pull/90/… I'd like f.config<AuthorInput> to dictate the f.value() type; Zod doesn't rely on inference b/c their z.string() is already correctly typed.

嗨@jcalz!我已经看到了47599,这很有意义,也许我会在那里添加我的用例……举个例子,我的库的API目前需要输入ADT/对象文字,比如{type:“Value”|“List”|...},而IMO Zod风格的流畅API已经变得流行起来,所以我正在尝试模仿它。如果你好奇的话:github.com/homebound-team/form-state/pull/90/…我希望f.config指定f.value()类型;Zod不依赖于推理b/c,他们的z.string()已经正确键入。

I'm not particularly inclined to look at external code unless it fits in a single file in my IDE. Could you provide a self-contained minimal reproducible example that motivates the generics in the question itself? If, at runtime, c.value() is a valid ValueConfig<V> for all possible V, then ValueConfig<V> should probably not be generic at all. But without something I can easily poke at I won't speculate further.

我并不特别倾向于查看外部代码,除非它适合我的IDE中的单个文件。你能提供一个独立的、最小的可重现的例子来激发问题本身中的泛型吗?如果在运行时,c.Value()对于所有可能的V都是有效的ValueConfig,那么ValueConfig可能根本不应该是泛型的。但如果没有我可以轻易戳到的东西,我就不会做进一步的推测。

Totally get not looking at external code, just wasn't sure how to give more context--but I did put slightly more context in TS#47599 i.e. it adds a ValueConfig.validate method that shows the types need to match for the lambda to type-check (as, you're right, in the SO question the generic-ness of ValueConfig doesn't seem necessary--I'll try to edit the SO question to show that). Fwiw happy to give you the answer here, i.e. "yes this is a limitation of context sensitive inference". Thanks!

完全不看外部代码,只是不确定如何提供更多的上下文-但我确实在TS#47599中放置了更多的上下文,即它添加了一个ValueConfig.validate方法,显示需要匹配的类型以进行类型检查(因为,你是对的,在SO问题中,ValueConfig的泛型似乎不必要-我将尝试编辑SO问题来显示)。Fwiw很乐意在这里给你答案,即“是的,这是上下文敏感推理的限制”。谢谢!

优秀答案推荐
更多回答

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com