Processing method, system and equipment for converting typescript type declaration code and medium
The API documents in HTML format are parsed through the cheatio library and natural language processing technology, and the data types are automatically identified and annotated information is generated, which solves the problem that existing tools cannot effectively convert HTML to typescript type declarations, and realizes efficient and accurate type declaration code generation to ensure that the code complies with project specifications and readability.
Patent Information
- Application Number
- CN202510281308.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-03-11
- Publication Date
- 2025-07-22
AI Technical Summary
Existing conversion tools cannot effectively extract interface description information from HTML-format API documents and identify data types in different fields, and cannot automatically add comment information, resulting in the converted typescript type declaration that does not comply with project specifications, affecting the readability and maintainability of the code.
The cheatio library is used to parse the HTML format API document, obtain structured interface description data through recursive functions, identify the data type using preset matching rules, and generate field annotation information through natural language processing technology. Finally, the API document is converted into typescript type declaration code and check and optimized.
It realizes automated conversion from HTML documents to typescript type declarations, improves the accuracy and efficiency of the conversion, ensures that the type declaration complies with project specifications, and enhances the readability and maintainability of the code.
Smart Images

Figure CN120353445A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of code processing, and in particular to a processing method, system, device and medium for converting TypeScript type declaration code. Background Art
[0002] In front-end development, as a strongly typed language, TypeScript provides developers with powerful functions such as type checking and code intelligent prompts, greatly improving development efficiency and code quality. However, when front-end developers interface with the back-end, they usually need to manually write a large number of TypeScript type declarations to describe the parameter types and return value types of the interfaces. This process is not only time-consuming and laborious, but also error-prone. Especially when there are many interface fields, such as a common CRUD interface that may contain 50 fields, manually writing type declarations may take 10 minutes or even longer.
[0003] To solve this problem, some conversion tools have emerged in the front-end technology community, such as json-to-ts, which can convert JSON data into TypeScript type declarations. However, the prerequisite for using these tools is that there needs to be a standard JSON data. But in actual development, the back-end API documents are often provided in HTML format rather than the standard JSON format. Therefore, the use of these tools is greatly limited in actual applications.
[0004] In order to convert HTML-formatted API documents into TypeScript type declarations, developers need a method that can parse HTML documents and extract interface description information. However, existing HTML parsing tools and methods can often only extract the text content in HTML documents and cannot effectively identify and structure interface description information. In addition, even if the interface description information can be extracted, how to accurately identify the data types of different fields is also a difficult problem. For example, how to distinguish data types such as int, string, date, etc., and how to handle the non-standard descriptions that may exist in back-end API documents are all key factors affecting the conversion accuracy.
[0005] In addition, in actual development, the code specifications of the project are also very important. If the converted TypeScript type declarations do not conform to the code specifications of the project, then manual correction is required, which will undoubtedly increase the workload of developers. Therefore, how to ensure that the converted TypeScript type declarations conform to the code specifications of the project is also an urgent problem to be solved.
[0006] In addition, to improve the readability and maintainability of the code, developers usually add annotation information to fields. However, existing conversion tools often cannot automatically add annotation information to fields, which also limits the effectiveness of conversion tools in practical applications.
[0007] In summary, existing conversion tools and methods have many limitations and deficiencies when converting HTML-formatted API documents into TypeScript type declarations. Summary of the Invention
[0008] The object of the present invention is to provide a processing method, system, device and medium for converting TypeScript type declaration code, which realizes the automatic conversion from HTML documents to TypeScript type declarations, improves the accuracy and efficiency of code conversion, enhances the readability and maintainability of the code, and provides reliable type support for API interface development, so as to solve at least one of the above-mentioned problems in the prior art.
[0009] In the first aspect, the present invention provides a processing method for converting TypeScript type declaration code, and the method specifically includes:
[0010] Use the cheerio library to parse the HTML-formatted API document, and traverse the text nodes of the parsed API document through a recursive function to obtain structured interface description data;
[0011] Based on preset matching rules, judge the interface description data to determine the TypeScript data types corresponding to all fields in the interface description data;
[0012] According to the field names in the interface description data and a preset annotation template, extract keywords from the interface description data through natural language processing technology to generate annotation information for the fields;
[0013] According to the TypeScript data types and annotation information corresponding to all fields in the interface description data, convert the API document into TypeScript type declaration code;
[0014] Through verification and optimization of the TypeScript type declaration code, an optimized TypeScript type declaration code is formed.
[0015] In the second aspect, the present invention provides a processing system for converting TypeScript type declaration code, and the system specifically includes:
[0016] The first processing module is used to parse the HTML - formatted API document using the cheerio library, and traverse the text nodes of the parsed API document through a recursive function to obtain structured interface description data;
[0017] The second processing module is used to judge the interface description data based on a preset matching rule to determine the typescript data types corresponding to all fields in the interface description data;
[0018] The third processing module is used to extract keywords from the interface description data according to the field names in the interface description data and a preset annotation template, and generate annotation information for the fields through natural language processing technology;
[0019] The fourth processing module is used to convert the API document into typescript type declaration code according to the typescript data types and annotation information corresponding to all fields in the interface description data;
[0020] The fifth processing module is used to form optimized typescript type declaration code by verifying and optimizing the typescript type declaration code.
[0021] In a third aspect, the present invention provides a computer device, including: a memory, a processor, and a computer program stored on the memory. When the computer program is executed on the processor, it implements the processing method for converting typescript type declaration code as described in any one of the above - mentioned methods.
[0022] In a fourth aspect, the present invention provides a computer - readable storage medium, on which a computer program is stored. When the computer program is run by a processor, it implements the processing method for converting typescript type declaration code as described in any one of the above - mentioned methods.
[0023] Compared with the prior art, the present invention has at least one of the following technical effects:
[0024] 1. The present invention realizes the automatic conversion from an HTML document to a typescript type declaration, improves the accuracy and efficiency of code conversion, enhances the readability and maintainability of the code, and provides reliable type support for API interface development.
[0025] 2. The present invention avoids the cumbersome process of manually writing typescript type declarations by automatically parsing HTML - formatted API documents and extracting interface description information, and greatly improves the conversion efficiency.
[0026] 3. Through the preset matching rules and natural language processing technology, the present invention can accurately identify the data types of different fields and process the irregular descriptions that may exist in the back-end API documentation, thereby improving the accuracy of conversion.
[0027] 4. By obtaining the project specification document and extracting the code specification details, the present invention converts the code specification details into executable code verification rules, and verifies and processes the converted typescript type declarations to ensure that the conversion results comply with the project's code specifications.
[0028] 5. By using natural language processing technology to extract keywords in the interface description data and generating annotation information for fields based on the preset annotation template, the present invention improves the readability and maintainability of the code.
[0029] 6. By using the cheerio library to parse the HTML-format API documentation and combining recursive functions to traverse text nodes, the present invention realizes the structured extraction of interface description data in the API documentation, improves the accuracy and efficiency of data extraction, and provides a reliable basis for subsequent type declaration code conversion.
[0030] 7. By constructing a DOM tree structure model, recursively traversing text nodes, and performing feature extraction, cleaning, and semantic clustering, the present invention forms structured interface description data, effectively processes the complex structures and information in HTML documents, and improves the intelligent level of data processing.
[0031] 8. By establishing a data type mapping table and based on the field data type information in the interface description data, the present invention realizes the automatic conversion of fields to typescript data types. For data types that cannot be directly mapped, through constraint information analysis, the type restriction results are further determined, improving the accuracy and flexibility of type conversion.
[0032] 9. By using an industry terminology library and the TF-IDF algorithm, combined with an annotation template library and the cosine similarity algorithm, the present invention realizes the automatic generation of field annotation information, which not only improves the accuracy and relevance of the annotation information but also reduces the workload of manually writing annotations.
[0033] 10. By mining and analyzing the field types and annotation information in the interface description data, the present invention forms a data conversion situation classification data set and uses a decision tree algorithm for modeling training to realize the automatic conversion of API documentation to typescript type declaration code, improving the intelligent and automated level of code conversion and reducing the need for manual intervention.
[0034] 11. The present invention optimizes the verification of TypeScript type declaration code by obtaining project specification documents, extracting code specification details, and converting them into executable code verification rules, ensuring that the code complies with project specifications and improving the readability and maintainability of the code.
[0035] 12. The present invention sets up unit test cases for the optimized TypeScript type declaration code by using the Jest test framework, ensuring the correctness and stability of the code, helping to promptly discover and fix potential problems in the code, and improving the quality and reliability of the code. BRIEF DESCRIPTION OF THE DRAWINGS
[0036] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the following will briefly introduce the drawings required for use in the embodiments. Obviously, the drawings in the following description are only some embodiments of the present application. For those of ordinary skill in the art, without creative efforts, other drawings can be obtained based on these drawings.
[0037] Figure 1 It is a schematic flowchart of a method for converting TypeScript type declaration code provided by the first embodiment of the present invention;
[0038] Figure 2 It is a schematic flowchart of a method for converting TypeScript type declaration code provided by the second embodiment of the present invention;
[0039] Figure 3 It is a schematic flowchart of a method for converting TypeScript type declaration code provided by the third embodiment of the present invention;
[0040] Figure 4 It is a schematic flowchart of a method for converting TypeScript type declaration code provided by the fourth embodiment of the present invention;
[0041] Figure 5 It is a schematic flowchart of a method for converting TypeScript type declaration code provided by the fifth embodiment of the present invention;
[0042] Figure 6 It is a schematic flowchart of a method for converting TypeScript type declaration code provided by the sixth embodiment of the present invention;
[0043] Figure 7 It is a schematic flowchart of a method for converting TypeScript type declaration code provided by the seventh embodiment of the present invention;
[0044] Figure 8It is a schematic structural diagram of a processing system for converting TypeScript type declaration code provided by an embodiment of the present invention;
[0045] Figure 9 It is a schematic structural diagram of a computer device provided by an embodiment of the present invention. Detailed implementation manners
[0046] In the following description, specific details such as specific system structures and technologies are presented for the purpose of illustration rather than limitation, so as to thoroughly understand the embodiments of the present application. However, those skilled in the art should clearly understand that the present application can also be implemented in other embodiments without these specific details. In other cases, detailed descriptions of well-known systems, devices, circuits, and methods are omitted to avoid unnecessary details from interfering with the description of the present application.
[0047] It should be understood that when used in the specification and claims of the present application, the term "comprising" indicates the presence of the described features, wholes, steps, operations, elements, and / or components, but does not exclude the presence or addition of one or more other features, wholes, steps, operations, elements, components, and / or their combinations.
[0048] It should also be understood that the term "and / or" as used in the specification and claims of the present application refers to any combination and all possible combinations of one or more of the associated listed items, and includes these combinations.
[0049] As used in the specification and claims of the present application, the term "if" can be interpreted as "when", "once", "in response to a determination", or "in response to a detection" depending on the context. Similarly, the phrase "if a determination is made" or "if [the described condition or event] is detected" can be interpreted as meaning "once a determination is made", "in response to a determination", "once [the described condition or event] is detected", or "in response to a detection of [the described condition or event]" depending on the context.
[0050] In addition, in the description of the specification and claims of the present application, the terms "first", "second", "third", etc. are only used for distinguishing descriptions and cannot be understood as indicating or implying relative importance.
[0051] References to "an embodiment" or "some embodiments" etc. described in the specification of this application mean that specific features, structures or characteristics described in connection with that embodiment are included in one or more embodiments of this application. Thus, statements such as "in one embodiment", "in some embodiments", "in other some embodiments", "in still other embodiments" etc. that appear in different places in this specification do not necessarily all refer to the same embodiment, but mean "one or more but not all embodiments", unless otherwise specifically emphasized in another way. The terms "comprise", "include", "have" and their variants all mean "include but not limited to", unless otherwise specifically emphasized in another way.
[0052] In an embodiment of this application, the execution subject of the process includes a terminal device. The terminal device includes but is not limited to: devices such as servers, computers, smart phones, and tablet computers that can execute the methods disclosed in this application. Figure 1 The flowchart showing the processing method for converting typescript type declaration code disclosed in the first embodiment of the present invention is shown below and is described in detail as follows:
[0053] S101, Parse the HTML-formatted API document using the cheerio library, and traverse the text nodes of the parsed API document through a recursive function to obtain structured interface description data.
[0054] In this embodiment, assume there is an HTML-formatted API document that describes multiple API interfaces, including the request method (GET, POST, etc.), path, description, parameters, and response of each interface. The goal is to parse this HTML document using the cheerio library and traverse its text nodes using a recursive function to extract structured interface description data. Specifically, use the cheerio library of Node.js to load the HTML document. Cheerio is a fast, flexible library that implements a subset of the core jQuery, and it allows operating on HTML documents in a way similar to jQuery; in the HTML document, API interfaces are usually wrapped in specific HTML elements, for example <div class="api-endpoint">, first locate these nodes containing API interface descriptions; for each API interface node, recursively traverse its child nodes, which includes finding text nodes that describe the interface's functionality (e.g., text within the label), look up the parameter list (which may be located in or (within the label), and a lookup response example (which may be located at <pre>inside the tag); during the traversal, extract the key information of each interface, such as the request method, path, description, parameters (including parameter names, types, and descriptions), and response examples. This information is organized into a structured data format, such as a JSON object; finally, output the extracted structured data to the console, a file, or other storage media for subsequent use.
[0055] In this embodiment, through the cheerio library and recursive functions, interface description data can be automatically extracted from HTML-formatted API documents, greatly improving efficiency. Since data is directly parsed and extracted from the HTML document, errors that may occur during the manual copy-paste process are avoided.
[0056] S102, based on a preset matching rule, judge the interface description data to determine the typescript data types corresponding to all fields in the interface description data.
[0057] In this embodiment, first, a set of matching rules need to be defined to map information such as field names, field descriptions, or field values in the interface description data to typescript data types. These rules can be formulated based on specific patterns of field names, keywords in field descriptions, or the formats of field values. For example, if the field name ends with _id, the data type is string (assuming the ID is in string form); if the field description contains keywords such as "date" or "time", the data type is Date or string (depending on the actual storage format); if the field value is in numeric form, the data type is number; if the field value is a boolean value (true / false), the data type is boolean; if the field value is an object or an array, the structure of the object or the element types of the array need to be further analyzed, and these rules are applied recursively.
[0058] Next, traverse each field in the interface description data and judge its data type according to the preset matching rules. This requires parsing the field name, field description, and field value and making a match according to the conditions in the rules. For each field, once its typescript data type is determined, this information is recorded, which can be achieved by adding a new field in the interface description data, such as tsType, to store the corresponding typescript data type. Finally, these fields and their corresponding typescript data types can be used to generate a typescript interface. This usually involves combining the field names and data types into the form of typescript interface syntax and outputting it to a.ts file.
[0059] In this embodiment, through a preset matching rule, we can automatically infer the corresponding TypeScript data type for the fields in the interface description data, reducing the workload of manually defining types and the possibility of errors. Using TypeScript types can improve the readability, maintainability, and security of the code. By automatically generating TypeScript interfaces, we can ensure that the data types of the API interfaces are correctly represented and verified in the code.
[0060] S103. According to the field names in the interface description data and a preset annotation template, extract the keywords in the interface description data through natural language processing technology to generate annotation information for the fields.
[0061] In this embodiment, first, a set of annotation templates needs to be designed. These templates define the basic structure and content format of the annotations. For example, a simple annotation template may include parts such as the purpose of the field, value range, and precautions, and placeholders are left in these parts for subsequent insertion of keywords or phrases extracted from the field description. Next, use natural language processing technology to perform word segmentation, part-of-speech tagging, and keyword extraction on the field description. This can be achieved by calling an NLP library or API, which can analyze the text content, identify important nouns, verbs, adjectives, etc., and extract them as keywords. After having the keywords, fill these keywords into the corresponding placeholders in the annotation template. Some logic is needed to handle the mapping relationship between the keywords and the template placeholders to ensure that the keywords are correctly placed in the appropriate positions in the annotation. Finally, convert the filled annotation template into the final annotation information, which involves replacing the placeholders in the template with the actual keywords and formatting the annotation to ensure that it meets the specifications and requirements of code annotations.
[0062] In this embodiment, through natural language processing technology and a preset annotation template, detailed and accurate field annotation information can be automatically generated, avoiding omissions or errors that may occur when manually writing annotations. Automatically generating annotations can significantly reduce the time for developers to write annotations, enabling them to focus more on code implementation and optimization. Detailed and accurate annotations help improve the readability and maintainability of the code, making it easier for other developers to understand the function and purpose of the code.
[0063] S104. According to the typescript data types and annotation information corresponding to all the fields in the interface description data, convert the API document into typescript type declaration code.
[0064] In this embodiment, for each field in the interface description data, we generate corresponding TypeScript field declarations based on its name, TypeScript data type, and comment information. This usually involves combining the field name, type, and comment according to the syntax rules of TypeScript to form a complete field declaration string. The generated TypeScript type declaration code is saved in a.d.ts file. This file can be recognized and used by the TypeScript compiler to provide functions such as type checking and code hinting.
[0065] In this embodiment, by generating TypeScript type declaration code, we can utilize these type declarations in a TypeScript project to enhance the type safety of the code. This helps to catch potential type errors at the compilation stage, improving the quality and stability of the code. The type declarations provide detailed information about the fields of the API interface, including the field names, types, and comments. This information helps other developers to more easily understand the functionality and purpose of the code, improving the readability and maintainability of the code.
[0066] S105, optimize the typescript type declaration code by validating it to form an optimized typescript type declaration code.
[0067] In this embodiment, first use a TypeScript compiler or a similar tool to perform syntax validation on the generated type declaration code to ensure that the code has no syntax errors and conforms to the syntax specifications of TypeScript. Next, check the integrity of the type declarations, including ensuring that all fields have clear type declarations, there is no missing type information, and whether the inheritance relationship between types is correct. It is also necessary to check the accuracy of the type declarations, which involves verifying whether the type declarations correctly reflect the actual data structure and constraint conditions of the API interface. For example, check whether the array type correctly specifies the element type, whether the union type includes all possible values, and whether the generic type is used correctly. After confirming the integrity and accuracy of the type declarations, perform code optimization, including using more concise type expressions, refactoring complex type structures to improve readability, and adding additional comments to explain complex type logic. Finally, perform a consistency check to ensure that the type declarations are consistent with the descriptions in the API documentation and there are no type differences caused by misunderstandings or oversights.
[0068] In this embodiment, through checksum verification and optimization, we can ensure that the type declarations correctly reflect the data structure and constraints of the API interfaces, thereby enhancing the type safety of the code. This helps to catch potential type errors during the compilation phase and reduce the likelihood of runtime errors. The optimized type declaration code is more concise, clear, and easy to understand. This helps other developers to grasp the functionality and purpose of the code more quickly, improving the maintainability and extensibility of the code.
[0069] In some embodiments, referring to Figure 2 , in the above step S101, the cheerio library is used to parse the HTML-formatted API documentation, and a recursive function is used to traverse the text nodes of the parsed API documentation to obtain structured interface description data, which specifically includes:
[0070] S1011, Use the cheerio library to load the HTML-formatted API documentation and construct a DOM tree structure model;
[0071] S1012, Traverse all text nodes in the DOM tree structure model through a recursive function, obtain the content of the text nodes, and determine whether the content of the text nodes is interface description-related information. If so, perform feature extraction and cleaning to obtain the cleaned interface description-related information;
[0072] S1013, Organize the cleaned interface description-related information according to a hierarchical structure to obtain initial interface description data. Through word segmentation processing and keyword mapping of the initial interface description data, obtain the vectorized representation of the initial interface description data;
[0073] S1014, Based on the vectorized representation, use a clustering algorithm to perform semantic clustering on the initial interface description data to form structured interface description data.
[0074] In this embodiment, the cheerio library is used to load the API document in HTML format, construct a DOM tree structure model, and obtain a tree structure representation of the document. All nodes in the DOM tree model are traversed through a recursive function to obtain the type, attributes, and content information of each node. It is determined whether the currently traversed node is a text node. If it is a text node, the text content of the node is obtained. According to the preset interface description related information features, it is determined whether the obtained text content contains interface description information. If it contains, further processing is performed. For the filtered text content containing interface descriptions, natural language processing techniques are used for feature extraction to obtain features such as keywords and key phrases. According to the extracted features, the text content is cleaned and normalized to remove redundant information, and the cleaned interface description related information is obtained. According to the hierarchical relationship of the interfaces, the interface description information is organized into a tree structure to obtain the initial interface description data. The initial interface description data is tokenized, and the description information is segmented into several keywords. Through a pre-constructed keyword mapping table, the keywords are mapped into vector representations to obtain the vectorized representation of the interface description data. According to the vectorized representation of the interface description data, the semantic similarity between interfaces is calculated to construct a semantic similarity matrix. The hierarchical clustering algorithm is used, with semantic similarity as the distance metric, to cluster the interface description data, and interfaces with similar semantics are aggregated together to form several interface clusters. For each interface cluster, its central interface is extracted as the representative of the cluster, and other interfaces in the cluster are used as the lower-level interfaces of the central interface to construct the hierarchical structure of the interfaces. According to the clustering results and the hierarchical structure, structured interface description data is generated, including the basic information, parameter information, call relationship, etc. of the interfaces.
[0075] Exemplarily, when using a web parsing library to load the interface document, the document content can be constructed into a tree structure, where each node contains information such as a tag name, attributes, and child nodes. For example, an interface document contains content such as a title, description, and parameter description. The hierarchical relationship between each part can be clearly expressed through the tree structure. When traversing the text nodes, it is necessary to determine whether the node content contains interface-related information. For example, when encountering keywords such as "interface name", "request method", and "parameter description", it indicates that the node contains interface description information. For nodes containing interface information, the effective content needs to be extracted and cleaned, such as removing extra spaces and special characters. By organizing the cleaned interface descriptions hierarchically, a complete interface information structure can be constructed. Taking the user management interface as an example, the first layer is the interface name "User Registration", the second layer contains request methods such as "Send Mobile Verification Code" and "Submit Registration Information", and the third layer is specific parameter descriptions such as "Mobile Number", "Verification Code", and "Password". When performing word segmentation on the interface description, the dictionary-based word segmentation method can be used. For example, "User Registration Interface" can be segmented into "User", "Registration", and "Interface". Then, the word segmentation results can be mapped to vectors. The term frequency statistics method can be used to count the number of times each word appears in the document as the component values of the vector. When performing semantic clustering based on the vectorized representation, the distance metric method can be used. For example, if the cosine similarity of two interface description vectors is greater than a certain threshold, it is considered that they are semantically similar and can be grouped into one category. For example, the vector similarity between "User Registration" and "Member Registration" is relatively high and can be grouped into the registration type interface. While the vector similarity between "Order Query" and "Product List" is relatively low and should be grouped into different categories. Through clustering, the interfaces can be grouped according to their functions to form a structured interface document. For example, under the user management category, there are interfaces such as registration, login, and information modification, and under the order management category, there are interfaces such as placing an order, payment, and query. This structured organization method facilitates developers to quickly locate and understand the interface functions. The advantage of this method is that it can automatically extract information from unstructured interface documents and perform semantic analysis, improving the readability and maintainability of the interface documents. At the same time, through semantic clustering, the association relationships between interfaces can be discovered, providing a reference for the reconstruction and optimization of the interfaces.
[0076] In some embodiments, referring to Figure 3 , in the above step S102, the determining the typescript data types corresponding to all fields in the interface description data based on a preset matching rule specifically includes:
[0077] S1021, establishing a data type mapping table, where the data type mapping table includes the field mapping relationships between common data types and typescript data types;
[0078] S1022. Based on the data type mapping table, convert different fields in the interface description data into corresponding TypeScript data types according to the data type information corresponding to different fields in the interface description data;
[0079] S1023. If the data type of any field is a data type that does not exist in the data type mapping table, then confirm that this field is a field of any data type;
[0080] S1024. Obtain the constraint information of all fields of any data type, and determine the type restriction result of the corresponding any data type according to whether the constraint information contains array feature descriptions and object feature descriptions.
[0081] In this embodiment, obtain the correspondence between common data types and TypeScript data types, construct a data type mapping table, and store field type conversion information. Analyze the interface description data, extract the data type information of each field, and find the corresponding TypeScript type in the mapping table according to the field name. If the TypeScript type corresponding to the field is found in the mapping table, then convert the data type of this field into a TypeScript type; otherwise, retain the original data type. For the converted TypeScript type, perform format verification according to the type characteristics to ensure that the field value conforms to the data format requirements of this type. By recursively traversing all fields of the interface description data, complete the type conversion of the entire interface data to obtain the converted TypeScript interface definition. Compare the converted TypeScript interface definition with the original interface description data to verify the correctness of the type conversion and ensure that the converted interface is consistent with the original interface structure.
[0082] Traverse each field and determine whether the data type of the field exists in the data type mapping table: if it exists, then confirm that the data type of this field is the corresponding data type in the mapping table; if it does not exist, then confirm that the data type of this field is a field of any data type; for the fields confirmed to be of any data type, obtain their constraint information; analyze the constraint information to determine whether it contains array feature descriptions and object feature descriptions to obtain the type restriction result; according to the type restriction result, use the corresponding data processing method to parse and convert the data of fields of any type; store the converted field data into the corresponding data structure to complete the automatic recognition and conversion process of field data types.
[0083] Determine the type restriction result of the corresponding any data type according to whether the constraint information contains array feature descriptions and object feature descriptions, specifically including:
[0084] Array Feature: If a field has an array feature description, then its type is restricted to Array <unknown>, indicating that this is an array, but the element type in the array is unknown.
[0085] Object feature: If a field has an object feature description, then its type is restricted to Record<string, unknown>, indicating that this is an object whose keys are of string type, but the value type is unknown.
[0086] Array and object feature: If a field has both an array feature description and an object feature description, then its type is restricted to Array<Record<string, unknown>>, indicating that this is an array where each element in the array is an object whose keys are of string type, but the value type of the object is unknown.
[0087] No feature: If the description information of a field contains neither an array feature description nor an object feature description, then its type is restricted to unknown, indicating that the type is unknown.
[0088] In this way, fields that might originally be of type any can be converted into more specific and secure types, thereby improving the quality and maintainability of the code.
[0089] Exemplarily, in the user management interface, the user name and password fields are of string type, the age field is of numeric type, and the enabled field is of boolean type. When performing type conversion, it is necessary to analyze the data type information of each field in the interface description data. Taking the product information interface as an example, text fields such as product name and description are converted to string type, numeric fields such as product price and inventory are converted to numeric type, and flag fields such as on-shelf status are converted to boolean type. For complex data structures, such as product specification information, it may contain multi-level nested objects and arrays. When encountering an unrecognized data type, it is marked as any type. This situation usually occurs in custom complex data structures. For example, the order status may be represented by an enumeration type, and the payment method may be represented by a union type. These types may not find corresponding relationships in the standard data type mapping table. For fields marked as any type, it is necessary to further analyze their constraint information to determine the specific type. By checking the feature description of the field, it can be determined whether it is an array or object type. Taking the product evaluation data as an example, the evaluation list has the characteristics of an array, containing multiple evaluation records; each evaluation record is an object, containing attributes such as evaluation content and score. The analysis of constraint information can help determine more precise type restrictions. For example, for the delivery address list field, through the constraint information, it can be determined that it is an array type of address objects, rather than simply any type. The address object contains attributes such as province, city, district, detailed address, and contact person. Another example is the product label field, through the constraint information, it can be determined that it is an array type of strings. This method of type mapping and constraint analysis can improve the type security of the interface document. By establishing a complete type system, type errors can be detected early in the development stage, reducing runtime errors. At the same time, clear type definitions also improve the maintainability and readability of the code. For complex business systems, accurate type information can help developers better understand the data structure and business logic.
[0090] In some embodiments, referring to Figure 4 , in the above step S103, the method of extracting keywords from the interface description data according to the field names in the interface description data and a preset annotation template, and generating annotation information for the fields by natural language processing technology specifically includes:
[0091] S1031, based on a pre-constructed industry term library, calculate the similarity between each field name in the interface description data and each term in the industry term library, classify the field names with a similarity higher than a first preset similarity threshold to at least one term in the industry term library into a first field name set, and classify the field names with a similarity lower than a second preset similarity threshold to each term in the industry term library into a second field name set;
[0092] S1032. Use the term with the highest similarity corresponding to each field name in the first field name set as the keyword for that field name, and form a first keyword set;
[0093] S1033. Use the TF-IDF algorithm to extract keywords for each field name in the second field name set to obtain a second keyword set;
[0094] S1034. Based on search metrics, use the cosine similarity algorithm to calculate the matching degree score of each annotation template in the pre-constructed annotation template library according to the first keyword set and the second keyword set, and obtain a matching degree score result. The search metrics include keyword coverage, semantic similarity, and context relevance;
[0095] S1035. According to the matching degree score result, determine the target annotation template with the highest matching degree score, and fill the first keyword set and the second keyword set into the target annotation template to generate annotation information for the field.
[0096] In this embodiment, a pre-constructed industry term library and interface description data to be processed are obtained. For each field name in the interface description data, its similarity with each term in the industry term library is calculated. A first preset similarity threshold and a second preset similarity threshold are set. According to the calculated similarity between each field name and the terms, the field names with similarity higher than the first threshold are classified into the first field name set, and the field names with similarity lower than the second threshold are classified into the second field name set. For each field name in the first field name set, the term with the highest similarity in the term library is determined, and this term is used as the keyword of this field name, finally forming the first keyword set. For the second field name set, the TF-IDF algorithm is used to extract keywords for each field name in it. By calculating the term frequency and inverse document frequency of the words in the field name, keywords with higher weights are obtained, finally forming the second keyword set. The first keyword set and the second keyword set are used as the input data for calculating the matching degree score. For each annotation template in the pre-constructed annotation template library, the cosine similarity algorithm is used to calculate its matching degree score with the keyword set. The search metrics include keyword coverage, semantic similarity, and context relevance; according to the keyword coverage, judge the proportion of the number of keywords included in the annotation template in the keyword set. The higher the coverage, the higher the matching degree score; according to the semantic similarity, use the Word2Vec word vector model to calculate the semantic similarity degree between the annotation template and the keyword set. The higher the similarity, the higher the matching degree score; according to the context relevance, through the LSTM neural network model, judge the relevance degree between the annotation template and the keyword set in the context. The higher the relevance, the higher the matching degree score; comprehensively obtain the final matching degree score result of each annotation template based on the scores of the three metrics of keyword coverage, semantic similarity, and context relevance; sort according to the matching degree score result, determine the annotation template with the highest matching degree score as the target annotation template, and fill the first keyword set and the second keyword set into the corresponding positions of the target annotation template to generate complete field annotation information.
[0097] Exemplarily, taking the e-commerce field as an example, the term library contains common terms such as "product", "order", "payment", etc. and their variants. When processing a product management interface, field names such as "product name" and "product price" have a high similarity to the "product" term in the term library and can be grouped into the first set of field names. Custom fields such as "promotion flag" and "inventory warning value" have a lower similarity to the term library terms and are grouped into the second set of field names. For the first set of field names, the most matching term can be found by calculating the similarity. For example, the similarity between the "product number" field and the "product" term in the term library is 0.8, and the similarity with the "number" term is 0.7. The "product" with the highest similarity is taken as the keyword. For the second set of field names, the term frequency-inverse document frequency algorithm is used to extract keywords. For example, for the "promotion flag" field, "promotion" is extracted as the keyword after analyzing the term frequency and importance. When searching for a matching annotation template, the keyword coverage rate reflects whether the template contains sufficient key information. For example, the product description template "used to record basic product information" contains the "product" keyword. The semantic similarity reflects the semantic matching degree between the template and the field. For example, the "inventory quantity" field has a high semantic similarity to the template "records the current sellable quantity of the product". The context relevance considers the business relationship between fields. For example, fields related to orders should preferably match templates for the order business scenario. By calculating the weighted scores of these metrics, the most suitable annotation template can be found. Taking the payment serial number field as an example, the templates with higher matching degrees include "unique identifier for payment transaction" and "order payment number", and the first template scores higher because it more accurately describes the purpose of the field. Filling the extracted keywords into the template can generate accurate annotation information. For example, filling the "payment" keyword into the "unique identifier for transaction" template generates the annotation "unique identifier for payment transaction". This annotation generation method based on the term library and templates can improve the standardization and understandability of the interface document. By term matching and keyword extraction, the consistency between the annotation and business terms is ensured. Using multi-dimensional search metrics ensures the selection of the most appropriate annotation template. Considering the association relationship between fields at the same time makes the annotation more in line with the business scenario. This not only improves the document quality but also facilitates developers to understand the meaning of the interface.
[0098] In some embodiments, referring to Figure 5 , in the above step S104, the converting the API document into a typescript type declaration code according to the typescript data types and annotation information corresponding to all fields in the interface description data specifically includes:
[0099] S1041. By mining and analyzing the typescript data types and annotation information corresponding to all fields in the interface description data, a data conversion scenario classification data set is formed. The data conversion scenario classification data set includes multiple different data conversion scenarios and the corresponding processing measures for each data conversion scenario. The processing measures include conversion algorithm selection, conversion parameter setting, and conversion threshold setting;
[0100] S1042. Using the data conversion scenario classification data set as input, a decision tree algorithm is employed for modeling and training to obtain a data conversion scenario classification model;
[0101] S1043. Based on the data conversion scenario classification model, the API document is converted into typescript type declaration code.
[0102] In this embodiment, according to the typescript data types and annotation information corresponding to the fields, possible data conversion scenarios are judged, and the corresponding processing measures for each scenario are determined, including conversion algorithm, parameter setting, and threshold setting. The data conversion scenarios and processing measures are used as labels, and the original data is used as features to construct a data conversion scenario classification data set. The decision tree algorithm is used, with the data conversion scenario classification data set as the training data, to train and generate a data conversion scenario classification model. For a given API document, the interface description data therein is extracted, and the trained classification model is used to predict the data conversion scenario of each field. According to the predicted conversion scenario, the corresponding processing measures are obtained, including the conversion algorithm to be adopted, the conversion parameters to be set, and the threshold. In accordance with the determined processing measures, the field definitions in the API document are converted into the corresponding TypeScript type declaration code.
[0103] Exemplarily, taking an e-commerce system as an example, the commodity price field usually uses a numerical type, and its annotation contains unit information such as "yuan" and "fen". Such fields need to be processed for unit conversion. The order status field is mostly an enumeration type, and the annotation contains the status value and its meaning description. It is necessary to map the status value to the corresponding type definition. The commodity specification field may use a string to store structured data, and the annotation describes its format specification, which needs to be parsed and converted into an object type. By analyzing these typical scenarios, conversion situations such as numerical unit conversion, enumeration mapping, and structured data parsing can be summarized. For numerical unit conversion, it is necessary to set the unit conversion ratio. For example, the conversion ratio from "yuan" to "fen" in RMB is one hundred. Enumeration mapping requires configuring the corresponding relationship between the status value and the type definition. For example, the order status "zero" is mapped to the "to be paid" type. Structured data parsing needs to set delimiters and parsing rules. For example, the commodity specification uses a semicolon to separate different attributes. Based on these conversion situations and processing measures, a decision tree model can be trained for classification. The model input features include field type, annotation keywords, data format features, etc. For example, the features of the price field may be: numerical type, containing the keyword "amount", and the data precision is two decimal places. The model output is the corresponding conversion processing scheme, including using a unit conversion converter, setting the conversion ratio to one hundred, etc. In practical applications, the model can accurately identify different conversion scenarios. For example, when encountering the commodity inventory field, by analyzing its numerical type feature and the unit keyword "piece", it is determined that no unit conversion is required. For the payment method field, according to its enumeration type feature and the annotation containing the status value list, the enumeration mapping conversion is selected. The user's delivery address field adopts the object type conversion because it contains structured data features. This classification method based on the decision tree can effectively handle various data conversion scenarios. Through semantic analysis of the field type and annotation, the conversion requirements can be accurately identified. According to different situations, appropriate conversion strategies are selected to ensure that the generated type declaration code accurately reflects the interface data structure. At the same time, considering business rules and data characteristics, the conversion result is more in line with the actual application requirements.
[0104] In some embodiments, referring to Figure 6 , in the above step S105, the forming of the optimized typescript type declaration code by verifying and optimizing the typescript type declaration code specifically includes:
[0105] S1051, obtaining a project specification document, extracting code specification details by analyzing the project specification document, and converting the code specification details into executable code verification rules;
[0106] S1052, using the code verification rules to perform verification processing on the typescript type declaration code, identifying code segments that do not conform to the code specification, and generating a code verification report;
[0107] S1053, repairing the code snippets in the typescript type declaration code that do not comply with the code specification according to the code verification report to form an optimized typescript type declaration code.
[0108] In this embodiment, the project specification document is semantically analyzed by natural language processing technology, and keywords and rule descriptions related to the code specification are extracted to form structured code specification details data. According to the code specification details data, the rule engine technology is used to convert the code specification details into executable code verification rules, and a code verification rule library is constructed. The typescript type declaration code to be verified is obtained, and the code is parsed into an abstract syntax tree through syntax analysis. The nodes of the abstract syntax tree are traversed, and the nodes are matched and verified according to the rules in the code verification rule library to determine whether the code fragment meets the specification requirements. For code fragments that do not meet the specification requirements, information such as their location, error type and error description is recorded, and a code verification report is generated. According to the code verification report, pattern matching and code refactoring technology are used to automatically repair the code fragments that do not meet the specifications, and the repaired typescript type declaration code is generated. The repaired code is compared with the original code to determine the optimized code fragments to form the final optimized typescript type declaration code.
[0109] Exemplarily, the project specification document usually contains specification requirements in multiple dimensions such as naming specifications, annotation specifications, and code formats. Taking the naming specification as an example, the interface type usually requires using the upper camel case naming and starting with a specific prefix. For example, the user-related interface type needs to start with "User". Variable naming requires using the lower camel case naming method and reflecting its data type. For example, boolean type variables should start with "is" or "has". By analyzing these specification requirements, the corresponding code verification rules can be extracted. In terms of the annotation specification, it is required that each interface type definition must include a complete annotation description. The annotation content needs to include elements such as function description, parameter description, usage examples, etc. For complex data structures, the value range and business meaning of each field also need to be explained. These specification requirements can be transformed into verification rules for annotation integrity and standardization. The code format specification includes specific requirements such as indentation, line breaks, and spaces. For example, the attributes in the type definition need to be indented by two spaces, there should be a blank line between attributes, and a line break is required at the end of the type definition. These format requirements can be converted into corresponding format verification rules. In actual applications, the type declaration code can be checked through a code verification tool. Taking the user information interface as an example, the tool will check whether the interface naming conforms to the "User" prefix specification, whether the attribute naming conforms to the lower camel case rule, whether the annotation is complete and standardized, and whether the code format meets the requirements, etc. For the code that does not conform to the specification, the tool will identify the specific location and problem description in the verification report. For the problems found in the verification, corresponding repairs need to be made. For example, if it is found that the interface type naming is not standardized, it needs to be modified to a conforming name. For the code lacking annotations, complete annotation descriptions need to be added. Format problems need to adjust typesetting details such as indentation and line breaks. Through these repair measures, it is ensured that the finally generated type declaration code fully conforms to the project specification requirements. This kind of standardization processing not only improves the readability and maintainability of the code, but also ensures that the code styles written by team members are unified. Through the automated verification and repair process, the workload of manual review can be reduced and the development efficiency can be improved. At the same time, the standardized code is also helpful for subsequent code reuse and system maintenance work.
[0110] In some embodiments, referring to Figure 7 , the method further includes:
[0111] S201, using the Jest test framework to set unit test cases for the optimized typescript type declaration code, where the unit test cases are used to verify whether the type declarations in the optimized typescript type declaration code can correctly describe the parameter types and return value types of the interface;
[0112] S202, performing unit tests on the optimized typescript type declaration code according to the unit test cases to obtain test results;
[0113] S203. Based on the test results, determine whether the unit test of the optimized TypeScript type declaration code fails. If it fails, correct the optimized TypeScript type declaration code according to the failure reason.
[0114] In this embodiment, according to the optimized TypeScript type declaration code, unit test cases are written using the Jest test framework. The test cases include the verification of the interface parameter types and return value types. By running the unit test cases, the test results are obtained to determine whether the test passes. If the test fails, analyze the failure reason to determine whether it is caused by an incorrect interface parameter type declaration or an incorrect return value type declaration. According to the failure reason, correct the optimized TypeScript type declaration code, focusing on correcting the incorrect parameter type declaration or return value type declaration. For the corrected TypeScript type declaration code, regenerate the unit test cases and execute the test again. Obtain the new test results to determine whether the corrected type declaration code passes the test. If there are still failures, repeat the above steps until all tests pass. Determine that the optimized TypeScript type declaration code passes the unit test verification and can correctly describe the parameter types and return value types of the interface, completing the optimization and testing of the type declaration.
[0115] Exemplarily, taking the user management system as an example, the user information interface type includes two parts: basic information and extended information. The basic information includes required fields such as user identification, name, age, etc., and the extended information includes optional fields such as avatar, address, etc. The test cases need to cover various type combinations of these fields. In test case design, first verify the type constraints of the required fields. By constructing test data of different types, check whether the type declaration can correctly identify type errors. For example, passing a numeric type username, or passing a string type age, the type check should report an error. For optional fields, it is necessary to verify the type check results in both cases where the field exists and does not exist. The test cases also need to verify the correctness of the interface return value type. The user query interface may return a single user information or a user list, and it is necessary to verify whether the return value type is consistent with the actual business. The data structure returned by the paging query interface contains information such as the total number and the current page, and it is necessary to verify whether the type definitions of these fields are accurate. When performing unit tests, the test framework will run all test cases and generate a test report. The test report details the execution results of each test case, including the number of passed and failed test cases and the specific failure reasons. For cases of type mismatch, the report will point out the specific location of the type error. When a test failure is found, it is necessary to correct it according to the failure reason. Too strict type definitions may cause valid data to be rejected, and it is necessary to appropriately relax the type constraints. For example, in addition to integers, user age may also be a decimal, and the type definition needs to support numeric types. Too loose type definitions may allow invalid data, and it is necessary to tighten the type constraints, such as sensitive information fields should be restricted to strings in a specific format. Through repeated testing and correction, ensure that the type declaration can accurately describe the parameter and return value types of the interface. This test-driven development method helps to improve the quality of type definitions. A perfect type declaration can not only detect type errors early in the development stage, but also serve as an interface document to help developers use the interface correctly.
[0116] Referring to Figure 8 , an embodiment of the present invention provides a processing system 8 for converting typescript type declaration code, and the system 8 specifically includes:
[0117] The first processing module 801 is used to parse the HTML format API document by using the cheerio library, and traverse the text nodes of the parsed API document through a recursive function to obtain structured interface description data;
[0118] The second processing module 802 is used to judge the interface description data based on a preset matching rule to determine the typescript data types corresponding to all fields in the interface description data;
[0119] The third processing module 803 is configured to extract keywords from the interface description data through natural language processing technology according to the field names in the interface description data and a preset annotation template, and generate annotation information for the fields;
[0120] The fourth processing module 804 is configured to convert the API document into typescript type declaration code according to the typescript data types and annotation information corresponding to all fields in the interface description data;
[0121] The fifth processing module 805 is configured to form optimized typescript type declaration code by verifying and optimizing the typescript type declaration code.
[0122] It can be understood that the content in the embodiment of the method for converting typescript type declaration code as Figure 1 shown is applicable to the embodiment of the system for converting typescript type declaration code. The functions specifically implemented by the embodiment of the system for converting typescript type declaration code are the same as those in the embodiment of the method for converting typescript type declaration code as Figure 1 shown, and the beneficial effects achieved are also the same as those in the embodiment of the method for converting typescript type declaration code as Figure 1 shown.
[0123] It should be noted that for the information interaction, execution process, etc. between the above systems, since they are based on the same concept as the method embodiment of the present invention, for their specific functions and the technical effects brought, reference can be made to the method embodiment part, and details are not described here again.
[0124] Those skilled in the art can clearly understand that for the convenience and brevity of description, only the above division of each functional unit and module is used as an example. In actual applications, the above functions can be allocated to different functional units and modules according to needs, that is, the internal structure of the system is divided into different functional units or modules to complete all or part of the functions described above. Each functional unit and module in the embodiment can be integrated into a processing unit, or each unit can exist physically alone, or two or more units can be integrated into one unit. The above integrated unit can be implemented in the form of hardware or in the form of a software functional unit. In addition, the specific names of each functional unit and module are only for the convenience of mutual distinction and do not limit the protection scope of the present application. The specific working processes of the units and modules in the above system can refer to the corresponding processes in the foregoing method embodiment, and details are not described here again.
[0125] Refer to Figure 9 , an embodiment of the present invention further provides a computer device 9, including: a memory 902, a processor 901, and a computer program 903 stored on the memory 902. When the computer program 903 is executed on the processor 901, it implements the method for converting typescript type declaration code as described in any one of the above methods.
[0126] The computer device 9 may be a computing device such as a desktop computer, a notebook, a palm computer, and a cloud server. The computer device 9 may include, but is not limited to, a processor 901 and a memory 902. Those skilled in the art can understand that Figure 9 merely an example of the computer device 9, which does not constitute a limitation on the computer device 9, and may include more or fewer components than shown in the figure, or combine certain components, or different components. For example, it may also include input / output devices, network access devices, etc.
[0127] The so-called processor 901 may be a central processing unit (CPU), and the processor 901 may also be other general-purpose processors, digital signal processors (DSPs), application-specific integrated circuits (ASICs), field-programmable gate arrays (FPGAs), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. The general-purpose processor may be a microprocessor or the processor may also be any conventional processor, etc.
[0128] The memory 902 may be an internal storage unit of the computer device 9 in some embodiments, such as the hard disk or memory of the computer device 9. The memory 902 may also be an external storage device of the computer device 9 in other embodiments, such as a plug-in hard disk, a smart media card (SMC), a secure digital (SD) card, a flash card, etc. equipped on the computer device 9. Further, the memory 902 may also include both the internal storage unit and the external storage device of the computer device 9. The memory 902 is used to store an operating system, application programs, a boot loader, data, and other programs, such as the program code of the computer program. The memory 902 may also be used to temporarily store data that has been output or will be output.
[0129] An embodiment of the present invention also provides a computer-readable storage medium, on which a computer program is stored. When the computer program is run by a processor, it implements the method for converting TypeScript type declaration code as described in any one of the above methods.
[0130] In this embodiment, if the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, to implement all or part of the processes in the above embodiment methods of the present application, a computer program can be used to instruct relevant hardware to complete. The computer program can be stored in a computer-readable storage medium. When the computer program is executed by a processor, the steps of the above various method embodiments can be implemented. Among them, the computer program includes computer program code, and the computer program code can be in the form of source code, object code, executable file or some intermediate form, etc. The computer-readable medium can at least include: any entity or device, recording medium, computer memory, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), electrical carrier signal, telecommunication signal, and software distribution medium that can carry the computer program code to the photographing device / terminal device. For example, a USB flash drive, a mobile hard disk, a magnetic disk, or an optical disc, etc. In some jurisdictions, according to legislation and patent practice, the computer-readable medium cannot be an electrical carrier signal and a telecommunication signal.
[0131] In the above embodiments, the descriptions of the various embodiments have their own emphases. For parts not detailed or recorded in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.
[0132] Those of ordinary skill in the art can realize that the units and algorithm steps of the examples described in combination with the embodiments disclosed in this document can be implemented by electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are executed in a hardware or software manner depends on the specific application and design constraints of the technical solution. Professional technicians can use different methods to implement the described functions for each specific application, but such implementation should not be considered to exceed the scope of this application.
[0133] In the embodiments disclosed in the present application, it should be understood that the disclosed device / terminal device and method can be implemented in other ways. For example, the device / terminal device embodiments described above are merely illustrative. For example, the division of the modules or units is only a logical function division. In actual implementation, there may be other division methods. For example, multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the displayed or discussed coupling or direct coupling or communication connection between each other can be through some interfaces. The indirect coupling or communication connection of the device or unit can be in electrical, mechanical or other forms.
[0134] The units described as separate components may or may not be physically separated. The components displayed as units may or may not be physical units, that is, they may be located in one place or distributed to multiple network units. Some or all of the units can be selected according to actual needs to achieve the purpose of the solution of this embodiment.< / unknown> < / pre>
Claims
1. A processing method for converting TypeScript type declaration code, characterized in that, The method specifically includes: Using the cheerio library to parse the API document in HTML format, and traversing the text nodes of the parsed API document through a recursive function to obtain structured interface description data; Based on preset matching rules, judge the interface description data to determine the typescript data types corresponding to all fields in the interface description data; According to the field names in the interface description data and a preset annotation template, extract keywords from the interface description data through natural language processing technology to generate annotation information for the fields; According to the typescript data types and annotation information corresponding to all fields in the interface description data, convert the API document into typescript type declaration code; Through checking and optimizing the typescript type declaration code, an optimized typescript type declaration code is formed.
2. The method according to claim 1, characterized in that, The step of using the cheerio library to parse the API document in HTML format, and traversing the text nodes of the parsed API document through a recursive function to obtain structured interface description data specifically includes: Using the cheerio library to load the API document in HTML format and construct a DOM tree structure model; Traversing all text nodes in the DOM tree structure model through a recursive function to obtain the content of the text nodes, and judging whether the content of the text nodes is interface description-related information. If so, perform feature extraction and cleaning to obtain the cleaned interface description-related information; Organize the cleaned interface description-related information according to the hierarchical structure to obtain initial interface description data, and obtain the vector representation of the initial interface description data through word segmentation processing and keyword mapping of the initial interface description data; Based on the vector representation, use a clustering algorithm to perform semantic clustering on the initial interface description data to form structured interface description data.
3. The method according to claim 1, characterized in that, The step of based on preset matching rules, judging the interface description data to determine the typescript data types corresponding to all fields in the interface description data specifically includes: Establish a data type mapping table, which includes the field mapping relationship between common data types and typescript data types; Based on the data type mapping table, according to the data type information corresponding to different fields in the interface description data, convert different fields in the interface description data into corresponding typescript data types; If the data type of any field is a data type not present in the data type mapping table, then confirm that the field is a field of the any data type; Obtain the constraint information of all fields of the any data type, and determine the type restriction result of the corresponding any data type according to whether the constraint information contains array feature descriptions and object feature descriptions.
4. The method according to claim 1, wherein According to the field names in the interface description data and a preset annotation template, extract the keywords in the interface description data through natural language processing technology to generate annotation information for the fields, specifically including: Based on a pre-constructed industry term library, calculate the similarity between each field name in the interface description data and each term in the industry term library. Classify the field names with a similarity higher than the first preset similarity threshold to at least one term in the industry term library into the first field name set, and classify the field names with a similarity lower than the second preset similarity threshold to each term in the industry term library into the second field name set; Use the term with the highest similarity corresponding to each field name in the first field name set as the keyword for this field name to form a first keyword set; Use the TF-IDF algorithm to extract keywords for each field name in the second field name set to obtain a second keyword set; Based on search metrics, according to the first keyword set and the second keyword set, use the cosine similarity algorithm to calculate the matching degree score of each annotation template in the pre-constructed annotation template library to obtain a matching degree score result. The search metrics include keyword coverage, semantic similarity, and context relevance; According to the matching degree score result, determine the target annotation template with the highest matching degree score, and fill the first keyword set and the second keyword set into the target annotation template to generate annotation information for the fields.
5. The method according to claim 1, characterized in that, According to the typescript data types and annotation information corresponding to all fields in the interface description data, convert the API document into typescript type declaration code, specifically including: Through mining and analyzing the typescript data types and annotation information corresponding to all fields in the interface description data, form a data conversion scenario classification data set. The data conversion scenario classification data set includes multiple different data conversion scenarios and the corresponding processing measures for each data conversion scenario. The processing measures include conversion algorithm selection, conversion parameter setting, and conversion threshold setting; Use the data conversion scenario classification data set as input, and adopt a decision tree algorithm for modeling training to obtain a data conversion scenario classification model; Based on the data conversion scenario classification model, convert the API document into typescript type declaration code.
6. The method according to claim 1, characterized in that Through verifying and optimizing the typescript type declaration code, form an optimized typescript type declaration code, specifically including: Obtain the project specification document, extract the code specification details by analyzing the project specification document, and convert the code specification details into executable code verification rules; Use the code verification rules to perform verification processing on the typescript type declaration code, identify the code segments that do not conform to the code specification, and generate a code verification report; Repair the code snippets in the TypeScript type declaration code that do not conform to the code specifications according to the code verification report to form an optimized TypeScript type declaration code.
7. The method according to any one of claims 1 to 6, characterized in that The method further includes: Using the Jest test framework to set unit test cases for the optimized TypeScript type declaration code, where the unit test cases are used to verify whether the type declarations in the optimized TypeScript type declaration code can correctly describe the parameter types and return value types of the interfaces; Perform unit tests on the optimized TypeScript type declaration code according to the unit test cases to obtain test results; Based on the test results, determine whether the unit test of the optimized TypeScript type declaration code fails. If it fails, correct the optimized TypeScript type declaration code according to the failure reason.
8. A processing system for converting TypeScript type declaration code, characterized in that, The system specifically includes: A first processing module for parsing the HTML-formatted API document using the cheerio library and traversing the text nodes of the parsed API document through a recursive function to obtain structured interface description data; A second processing module for judging the interface description data based on a preset matching rule to determine the TypeScript data types corresponding to all fields in the interface description data; A third processing module for extracting keywords from the interface description data through natural language processing technology according to the field names in the interface description data and a preset annotation template to generate annotation information for the fields; A fourth processing module for converting the API document into a TypeScript type declaration code according to the TypeScript data types and annotation information corresponding to all fields in the interface description data; A fifth processing module for forming an optimized TypeScript type declaration code by verifying and optimizing the TypeScript type declaration code.
9. A computer device, characterized in that, Includes: A memory, a processor, and a computer program stored on the memory. When the computer program is executed on the processor, it implements the method for converting a TypeScript type declaration code according to any one of claims 1 to 7.
10. A computer-readable storage medium, characterized in that, A computer program is stored thereon. When the computer program is run by the processor, it implements the method for converting a TypeScript type declaration code according to any one of claims 1 to 7.