We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
题目链接:LengthOfString,解答
实现 LengthOfString,获取字符串的长度:
LengthOfString
import type { Equal, Expect } from "@type-challenges/utils"; type cases = [ Expect<Equal<LengthOfString<"">, 0>>, Expect<Equal<LengthOfString<"kumiko">, 6>>, Expect<Equal<LengthOfString<"reina">, 5>>, Expect<Equal<LengthOfString<"Sound! Euphonium">, 16>> ];
type StringToArray<T extends string> = T extends `${infer F}${infer Rest}` ? [F, ...StringToArray<Rest>] : []; type LengthOfString<T extends string> = StringToArray<T>["length"];
js
length
ts
"aa"["length"]
number
type LengthOfString< T extends string, R extends string[] = [] > = T extends `${infer F}${infer Rest}` ? LengthOfString<Rest, [F, ...R]> : R["length"];
type LengthOfString<T extends string, R extends string[] = []> = T extends "" ? R["length"] : T extends `${infer F}${infer Rest}` ? LengthOfString<Rest, [F, ...R]> : never;
方法二、方法三的思路和方法一是一样的,区别在于:
The text was updated successfully, but these errors were encountered:
No branches or pull requests
题目
题目链接:LengthOfString,解答
实现
LengthOfString
,获取字符串的长度:答案
方法一
知识点
js
中计算字符串的长度,直接用length
就可以获取字符串的长度ts
类型中,"aa"["length"]
得到的结果是number
length
属性的还有元组类型length
属性,不就拿到元组的长度了么方法二
方法三
知识点
方法二、方法三的思路和方法一是一样的,区别在于:
The text was updated successfully, but these errors were encountered: