admin管理员组文章数量:1291108
Let's start simple.
I can do this in TypeScript to have a "kind of" Not
type:
type Not<S extends string> = Exclude<S, 'not'>
Next, let's test it:
type T1 = Not<'not'>; // => never
Works fine.
Now, I want to use it in a function:
function f<S extends string>(s: Not<S>) {}
f('not'); // => Error!
Perfect.
Now, onto the problem.
I want to make it work without generics:
function f(s: Not<string>) {}
f('not'); // => No Error :(
That obviously doesn't work.
For my use case, it's even worse, as I want to extract the argument directly:
type FstArg<T> = T extends (a: infer A) => any ? A : never;
type Arg = FstArg<typeof f> // => string
I wish Arg
would be something like Exclude<X?, 'not'>
.
I do understand why this doesn't work, but is there a workaround for this?
This would probably require a true Not
type, I guess?
Playground
本文标签: typescriptPreserving Exclude in Argument ExtractionStack Overflow
版权声明:本文标题:typescript - Preserving Exclude in Argument Extraction - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741506338a2382342.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论