Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript 有没有一种方法可以在条件类型脚本中使用扩展字符串进行推理?_Typescript_Inference - Fatal编程技术网

Typescript 有没有一种方法可以在条件类型脚本中使用扩展字符串进行推理?

Typescript 有没有一种方法可以在条件类型脚本中使用扩展字符串进行推理?,typescript,inference,Typescript,Inference,我试图提取一个泛型字符串文字类型,但typescript推断只返回类型字符串 所以从技术上讲,一旦字符串文本类型传递给函数,我们就不能再提取它了 type Key<T extends string> = { key: T }; declare function getKey<T extends string>(key: T): Key<T>; let someKey = getKey('check'); declare function updateWi

我试图提取一个泛型字符串文字类型,但typescript推断只返回类型字符串

所以从技术上讲,一旦字符串文本类型传递给函数,我们就不能再提取它了

type Key<T extends string> = { key: T };

declare function getKey<T extends string>(key: T): Key<T>;

let someKey = getKey('check');

declare function updateWithKey<T, K extends string>(key: T): T extends Key<K> ? K : never;

let someUpdatedKey = updateWithKey(someKey); // Shouldn't be 'check'?
type Key={Key:T};
声明函数getKey(key:T):key;
让someKey=getKey('check');
声明函数updateWithKey(键:T):T扩展键?K:从来没有;
让someUpdatedKey=updateWithKey(someKey);//不应该是“支票”吗?

这是
updateWithKey
中中的代码,
K
不会出现在编译器可以自动推断的位置,因此它被认为是允许的最通用的类型,即
string

但条件类型告诉编译器您希望推断实际类型:

type Key<T extends string> = { key: T };

declare function getKey<T extends string>(key: T): Key<T>;

let someKey = getKey('check');

declare function updateWithKey<T>(key: T): T extends Key<infer K> ? K : never;

let someUpdatedKey = updateWithKey(someKey); // let someUpdatedKey: "check"
type Key={Key:T};
声明函数getKey(key:T):key;
让someKey=getKey('check');
声明函数updateWithKey(键:T):T扩展键?K:从来没有;
让someUpdatedKey=updateWithKey(someKey);//让someUpdatedKey:“检查”

updateWithKey
中,
K
不会出现在编译器可以自动推断的位置,因此它被认为是允许的最通用类型,即
字符串

但条件类型告诉编译器您希望推断实际类型:

type Key<T extends string> = { key: T };

declare function getKey<T extends string>(key: T): Key<T>;

let someKey = getKey('check');

declare function updateWithKey<T>(key: T): T extends Key<infer K> ? K : never;

let someUpdatedKey = updateWithKey(someKey); // let someUpdatedKey: "check"
type Key={Key:T};
声明函数getKey(key:T):key;
让someKey=getKey('check');
声明函数updateWithKey(键:T):T扩展键?K:从来没有;
让someUpdatedKey=updateWithKey(someKey);//让someUpdatedKey:“检查”