英文:
Making second parameter optional based on specific value of a key type
问题
I have code like
interface MY_TYPES {
'xyz': string;
'abc': number;
'baz': undefined;
}
function testIt<T extends keyof MY_TYPES>(key: T, value?: MY_TYPES[T]) {
console.log(key);
if (value) {
console.log(value);
}
}
testIt('xyz', 123); // error
testIt('xyz'); // NO ERROR BY TS, but it should show error
testIt('xyz', 'str'); // good
testIt('baz'); // good
testIt('baz', 3); // error
In case of testIt('xyz')
, it's showing no error, but I want TypeScript to complain in such cases because MY_TYPES has some value for this key. However, TypeScript is not complaining here.
英文:
I have code like
interface MY_TYPES {
'xyz': string;
'abc': number;
'baz': undefined;
}
function testIt<T extends keyof MY_TYPES>(key: T, value?: MY_TYPES[T]) {
console.log(key);
if (value) {
console.log(value);
}
}
testIt('xyz', 123); // error
testIt('xyz'); // NO ERROR BY TS, but it should show error
testIt('xyz', 'str'); // good
testIt('baz'); // good
testIt('baz', 3); // error
In case of testIt('xyz')
Its showing no error, but I want TS to complain on such cases as MY_TYPES has some value for this key.
But TS is complaining here. playground
答案1
得分: 2
你可以使用rest parameter 并有条件地对参数元组进行类型化:
interface MY_TYPES {
'xyz': string;
'abc': number;
'baz': undefined;
}
function testIt<T extends keyof MY_TYPES>(
...args: MY_TYPES[T] extends undefined ? [T] : [T, MY_TYPES[T]]
) {
const [key, value] = args;
console.log(key);
if (value) {
console.log(value);
}
}
testIt('xyz'); // error
testIt('xyz', 123); // error
testIt('xyz', 'str'); // correct
testIt('baz'); // correct
testIt('baz', 3); // error
英文:
You could use a rest parameter and conditionally type the parameter tuple:
interface MY_TYPES {
'xyz': string;
'abc': number;
'baz': undefined;
}
function testIt<T extends keyof MY_TYPES>(
...args: MY_TYPES[T] extends undefined ? [T] : [T, MY_TYPES[T]]
) {
const [key, value] = args;
console.log(key);
if (value) {
console.log(value);
}
}
testIt('xyz'); // error
testIt('xyz', 123); // error
testIt('xyz', 'str'); // correct
testIt('baz'); // correct
testIt('baz', 3); // error
答案2
得分: 0
只需添加函数重载以提供可能的类型:
interface MY_TYPES {
'xyz': string;
'abc': number;
'baz': undefined;
}
function testIt(key: 'xyz', value: string): undefined
function testIt(key: 'abc', value: number): undefined
function testIt(key: 'baz'): undefined
function testIt<T extends keyof MY_TYPES>(key: T, value?: MY_TYPES[T]) {
console.log(key);
if (value) {
console.log(value);
}
}
testIt('xyz', 123); // 错误
testIt('xyz');
testIt('xyz', 'str'); // 正确
testIt('baz'); // 正确
testIt('baz', 3); // 错误
英文:
Just add function overloading to give possible types:
interface MY_TYPES {
'xyz': string;
'abc': number;
'baz': undefined;
}
function testIt(key: 'xyz', value: string) : undefined
function testIt(key: 'abc', value: number) : undefined
function testIt(key: 'baz') : undefined
function testIt<T extends keyof MY_TYPES>(key: T, value?: MY_TYPES[T]) {
console.log(key);
if (value) {
console.log(value);
}
}
testIt('xyz', 123); // error
testIt('xyz');
testIt('xyz', 'str'); // correct
testIt('baz'); // correct
testIt('baz', 3); // error
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论