泛型字符串上的Typescript条件类型
我有一个类型泛型字符串上的Typescript条件类型,typescript,typescript-typings,typescript-generics,Typescript,Typescript Typings,Typescript Generics,我有一个类型CodeGenBlock,我想根据type是root,null还是其他类型,将一些属性动态添加到对象的类型中: type InternalCodeGenTypes = 'root' | 'null'; export type CodeGenBlock< In extends string, T extends In | InternalCodeGenTypes = In | InternalCodeGenTypes > = { name: st
CodeGenBlock
,我想根据type
是root
,null
还是其他类型,将一些属性动态添加到对象的类型中:
type InternalCodeGenTypes = 'root' | 'null';
export type CodeGenBlock<
In extends string,
T extends In | InternalCodeGenTypes = In | InternalCodeGenTypes
> = {
name: string;
description: string;
signifier: string;
type: T;
opts?: CodeGenBlock<T>[];
data: T extends 'null' ? never : string | (() => string);
};
const myCodeBlock: CodeGenBlock<"userDefinedA" | "userDefinedB"> = {
type: "null",
// data is never never, I don't know why though
// I was wondering on on how to get the actual value of type without having to pass "null" as a parameter to CodeGenBlock
}
类型InternalCodeGenTypes='root'|'null';
导出类型代码块<
在字符串中,
T在| InternalCodeGenTypes中扩展=在| InternalCodeGenTypes中扩展
> = {
名称:字符串;
描述:字符串;
能指:弦;
类型:T;
选项?:代码块[];
数据:T扩展“null”?从不:string |(()=>string);
};
常量myCodeBlock:CodeGenBlock={
类型:“空”,
//数据永远不会,但我不知道为什么
//我想知道如何获得类型的实际值,而不必将“null”作为参数传递给CodeGenBlock
}
我试着做泛型并用extensions
测试它,但这并没有检查实际传递的值,只检查字符串列表,它总是返回true。我不知道我怎么能做到这一点
传入的类型是由软件用户定义的,因此我无法预定义并公开“root”和“null”也不起作用。您可以添加代码来解决您的问题吗?我不太明白你想用
CodeGenBlock
做什么。你不知道你想做什么。In type的用途是什么?@jcalz@Tim我需要根据提供给所述对象的type
属性的值修改CodeGenBlock对象的形状。我想知道是否可以这样做,而不必显式地将其作为参数传递给CodeGenBlock类型
@Tim In type接受用户输入,并允许使用库的内部参数对其进行扩展