简体   繁体   English

应该在打字稿的递归函数中使用哪种类型声明?

[英]Which type declaration should be used in a recursive function in typescript?

I am using a recursive function and currently declared my_json as an array of strings and then when run recursively as a type 'any', but obviously that's not a perfect solution.我正在使用递归函数,目前将 my_json 声明为一个字符串数组,然后在递归运行时作为“any”类型运行,但显然这不是一个完美的解决方案。 my_json has a specific type, but when the function is run recursively it changes its type to a index key. my_json 有一个特定的类型,但是当该函数递归运行时,它会将其类型更改为索引键。 How can I fix this?我怎样才能解决这个问题? Any advice appreciated.任何建议表示赞赏。

const findSomething = (my_json: any, value: string): boolean => {
    let exist = false;
    Object.keys(my_json).some(key => {
        exist =
            typeof my_json[key] === 'object'
                ? findSomething(my_json[key], value)
                : my_json[key] === value;
        return exist;
    });
    return exist;
};
findSomething(my_json, '123')
my_json: 
{
"aaa": [
"123",
"456",
"789"
],
"bbb": [
"000",
"001",
"002"
],
}

when the function is run recursively it changes its type to a index key当该函数以递归方式运行时,它会将其类型更改为索引键

Yes, when you call findSomething(my_json[key], value) , you are calling it with the index key value.是的,当您调用findSomething(my_json[key], value) ,您是使用索引键值调用它。


What you are calling my_json is an array.你所说的my_json是一个数组。 This naming confused me.这个命名让我很困惑。 I would expect something with that name to be a complex object or unknown type, not known to be an array.我希望具有该名称的东西是一个复杂的对象或未知类型,而不是一个数组。


Your problem is that you are confused.你的问题是你很困惑。 You cannot recurse into an array of strings, it's only "one level deep" because strings will not have iterable properties like an array or object.你不能递归到一个字符串数组中,它只是“一层深”,因为字符串不会像数组或对象那样具有可迭代的属性。

You recurse into arrays or objects.您递归到数组或对象。 First, clarify your understanding of the problem, then you will be able to fix the types.首先,澄清您对问题的理解,然后您将能够修复类型。


If you are using Object.keys , you seem to be expecting an object.如果您正在使用Object.keys ,您似乎在期待一个对象。 But you specified an array.但是你指定了一个数组。

Assuming that recursion is relevant to your problem and your my_json object is not an array as you typed it, here's an attempt at refactoring.假设递归与您的问题相关,并且您的my_json对象不是您键入的数组,那么这里尝试重构。

type Haystack: {
  [key: string]: string | Haystack
}

function find(needle: string, haystack: Haystack): boolean {
  if (typeof haystack === 'string') return needle === haystack
  return Object.keys(haystack).some(key => find(needle, haystack[key]))
}

I hope you find it instructive.我希望你觉得它很有启发性。 Good luck in your continuing education.祝你继续教育顺利。

I'm going to ignore that your function implementation doesn't make sense given what you've described as the input so far, but I will try to address the title of your question.鉴于您目前所描述的输入内容,我将忽略您的函数实现没有意义,但我会尝试解决您的问题的标题。

These are the Typescript function signature of JSON.parse and JSON.stringify , as written by the creators of Typescript:这些是JSON.parseJSON.stringify的 Typescript 函数签名,由 Typescript 的创建者编写:

parse(text: string, reviver?: (this: any, key: string, value: any) => any): any;

stringify(value: any, replacer?: (this: any, key: string, value: any) => any, space?: string | number): string;

Notice that any is all over the place as input arg and return type.请注意, any到处都是作为输入参数和返回类型。 That is the nature of a function that accepts arbitrary object input, which your function seems to expect to do as it recurses down the input when it checks typeof my_json[key] === 'object'?这是接受任意对象输入的函数的性质,您的函数似乎希望这样做,因为它在检查typeof my_json[key] === 'object'? . .

You can't do better than the Typescript creators.你不能比 Typescript 创建者做得更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM