[英]Typescript make the second parameter type depends on the first parameter type
enum Channel {
GITHUG = 'github',
GITLAB = 'gitlab',
}
interface Github {}
interface Gitlab {}
function foo (a, b) {}
第一个参数a是一个枚举(Channel)值
如果参数a是github,那么b的类型就是GitHub
如果参数a是gitlab,那么b的类型就是Gitlab
如何定义 function foo的类型?
使用条件类型技术。 查看下面的示例或 go 到在线游乐场以进行实际测试(感谢Joonas的帮助)
enum Channel {
GITHUG = 'github',
GITLAB = 'gitlab',
}
interface Github {
_foo: number
}
interface Gitlab {
_bar: number
}
type ParamType<C> = C extends Channel.GITHUG ? Github : Gitlab;
function foo<C extends Channel>(a: C, b: ParamType<C>): void {}
foo(Channel.GITLAB, { _foo: 1 }); // error
foo(Channel.GITLAB, { _bar: 1 }); // success
请让我知道它是否有效)
在这里,您有一些替代方法:
enum Channel {
GITHUB = 'github',
GITLAB = 'gitlab',
}
interface Github {
type: 'Github'
}
interface Gitlab {
type: 'Gitlab'
}
/**
* First approach
*/
type Params = [Channel.GITHUB, Github] | [Channel.GITLAB, Gitlab]
function foo(...args: Params) {
if (args[0] === Channel.GITHUB) {
const x = args[1] // Github
}
}
type Overloading =
& ((a: Channel.GITHUB, b: Github) => void)
& ((a: Channel.GITLAB, b: Gitlab) => void)
/**
* Second approach
*/
const foo2: Overloading = (a: Channel, b: Github | Gitlab) => null as any
const result = foo2(Channel.GITHUB, { type: 'Github' }) // ok
/**
* Third approach
*/
function foo3(a: Channel.GITLAB, b: Gitlab): void
function foo3(a: Channel.GITHUB, b: Github): void
function foo3(a: Channel, b: Github | Gitlab) {
}
foo3(Channel.GITLAB, { type: 'Gitlab' }) // ok
解释看代码注释:
enum Channel {
GITHUG = 'github',
GITLAB = 'gitlab',
}
interface Github {}
interface Gitlab {}
// Map enum value and type
interface Foo {
github: Github
gitlab: Gitlab
}
// `${Channel}` => 'github' | 'gitlab'
// Get the type K of a through the passed parameter
// if K is `github`, Foo[K] is Github
// if k is `gitlab`, Foo[K] is Gitlab
function foo<K extends `${Channel}`>(a: K, b: Foo[K]) {}
我有一个非常相似的问题,在我的例子中,第二个参数类型来自一个接口:
interface ArgTypes { [id: string]: any }
interface ChannelArgs extends ArgTypes{
github: {
hubhub: number
},
gitlab: {
lablab: number
},
...
}
type Arg<Args extends ArgTypes, Id extends keyof Args> = Args[Id]
我的foo
function 必须与实现接口的所有类型一起工作,所以它本身是通用的:
foo<ChannelArgs>('github', {hubhub: 12})
事实证明,泛型类型和推断类型的声明不会混合,但可以将它们放在不同的括号中:
type Foo<Args extends ArgTypes> = <Id extends keyof Args>(
id: Id,
arg: Arg<Args, Id>
) => void
const gitFoo: Foo<ChannelArgs> = function(a,b): void {}
gitFoo('github', {hubhub: 12}) // no error
gitFoo('github', {lablab: 12}) // error
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.