英文:
How to determine if a TypesScript type is a literal
问题
可能编写一个实用类型来确定一个类型是否为字面量吗?
例如:
type IsLiteral<T> ...
type IsStringALiteral = IsLiteral<string> // false
type IsStringLiteralALiteral = IsLiteral<'abc'> // true
type IsStringALiteral = IsLiteral<bool> // false
type IsStringLiteralALiteral = IsLiteral<false> // true
英文:
Is it possible to write a utility type to determine if a type is a literal?
e.g.
type IsLiteral<T> ...
type IsStringALiteral = IsLiteral<string> // false
type IsStringLiteralALiteral = IsLiteral<'abc'> // true
type IsStringALiteral = IsLiteral<bool> // false
type IsStringLiteralALiteral = IsLiteral<false> // true
答案1
得分: 0
以下是您提供的代码的中文翻译部分:
type IsString<T> = string extends T ? true : false;
type T = {
true: true,
123: 123,
'text': 'text',
string: string,
}
type X = {[K in keyof T]: IsString<T[K]>;}
// type X = {
// true: false;
// 123: false;
// text: false;
// string: true;
// }
英文:
type IsString<T> = string extends T ? true : false;
type T = {
true: true,
123: 123,
'text': 'text',
string: string,
}
type X = {[K in keyof T]: IsString<T[K]>}
// type X = {
// true: false;
// 123: false;
// text: false;
// string: true;
// }
答案2
得分: 0
以下是您要翻译的内容:
回答自己的问题,因为我已经弄清楚了:
type IsUnion<T, U extends T = T> = T extends unknown ? ([U] extends [T] ? false : true) : false;
type IsStringLiteral<A> = IsUnion<A> extends true ? false : A extends string ? (string extends A ? false : true) : false;
type IsNumberLiteral<A> = IsUnion<A> extends true ? false : A extends number ? (number extends A ? false : true) : false;
type IsBooleanLiteral<A> = IsUnion<A> extends true ? false : A extends boolean ? (boolean extends A ? false : true) : false;
type IsLiteral<A> = IsStringLiteral<A> extends true ? true : IsNumberLiteral<A> extends true ? true : IsBooleanLiteral<A> extends true ? true : false;
请注意,我在这里包含了IsUnion的功能,因为在我的情况下,我想要:
type IsStringUnionALiteral = IsLiteral<'a' | 'b'> // false
如果这不符合您的要求,那么可以如下:
type IsStringLiteral<A> = A extends string ? (string extends A ? false : true) : false;
type IsNumberLiteral<A> = A extends number ? (number extends A ? false : true) : false;
type IsBooleanLiteral<A> = A extends boolean ? (boolean extends A ? false : true) : false;
type IsLiteral<A> = IsStringLiteral<A> extends true ? true : IsNumberLiteral<A> extends true ? true : IsBooleanLiteral<A> extends true ? true : false;
英文:
Answering my own question here, since I've figured it out:
type IsUnion<T, U extends T = T> = T extends unknown ? ([U] extends [T] ? false : true) : false;
type IsStringLiteral<A> = IsUnion<A> extends true ? false : A extends string ? (string extends A ? false : true) : false;
type IsNumberLiteral<A> = IsUnion<A> extends true ? false : A extends number ? (number extends A ? false : true) : false;
type IsBooleanLiteral<A> = IsUnion<A> extends true ? false : A extends boolean ? (boolean extends A ? false : true) : false;
type IsLiteral<A> = IsStringLiteral<A> extends true ? true : IsNumberLiteral<A> extends true ? true : IsBooleanLiteral<A> extends true ? true : false;
Note here I have included IsUnion functionality because in my case I want:
type IsStringUnionALiteral = IsLiteral<'a' | 'b'> // false
If that doesn't suit your requirements, then it would be as follows:
type IsStringLiteral<A> = A extends string ? (string extends A ? false : true) : false;
type IsNumberLiteral<A> = A extends number ? (number extends A ? false : true) : false;
type IsBooleanLiteral<A> = A extends boolean ? (boolean extends A ? false : true) : false;
type IsLiteral<A> = IsStringLiteral<A> extends true ? true : IsNumberLiteral<A> extends true ? true : IsBooleanLiteral<A> extends true ? true : false;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论