简体   繁体   中英

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 has a specific type, but when the function is run recursively it changes its type to a index key. 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.


What you are calling my_json is an array. 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. 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.

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:

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. 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'? .

You can't do better than the Typescript creators.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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