代码之家  ›  专栏  ›  技术社区  ›  Borislav Ivanov

为什么有人会对属性使用JSON接口

  •  1
  • Borislav Ivanov  · 技术社区  · 4 年前

    当我盯着代码库看的时候,我发现了一个带有类型定义的接口:

    interface Something {
        prop: JSON;
    }
    

    哪里 JSON

    interface JSON {
    
        /**
    
         * Converts a JavaScript Object Notation (JSON) string into an object.
    
         * @param text A valid JSON string.
    
         * @param reviver A function that transforms the results. This function is called for each member of the object.
    
         * If a member contains nested objects, the nested objects are transformed before the parent object is.
    
         */
    
        parse(text: string, reviver?: (this: any, key: string, value: any) => any): any;
    
        /**
    
         * Converts a JavaScript value to a JavaScript Object Notation (JSON) string.
    
         * @param value A JavaScript value, usually an object or array, to be converted.
    
         * @param replacer A function that transforms the results.
    
         * @param space Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.
    
         */
    
        stringify(value: any, replacer?: (this: any, key: string, value: any) => any, space?: string | number): string;
    
        /**
    
         * Converts a JavaScript value to a JavaScript Object Notation (JSON) string.
    
         * @param value A JavaScript value, usually an object or array, to be converted.
    
         * @param replacer An array of strings and numbers that acts as a approved list for selecting the object properties that will be stringified.
    
         * @param space Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.
    
         */
    
        stringify(value: any, replacer?: (number | string)[] | null, space?: string | number): string;
    
    }
    
    
    
    /**
    
     * An intrinsic object that provides functions to convert JavaScript values to and from the JavaScript Object Notation (JSON) format.
    
     */
    
    declare var JSON: JSON;
    

    定义于 node_modules/typescript/lib/lib.es5.d.ts

    interface JSON {
    
        readonly [Symbol.toStringTag]: string;
    
    }
    

    定义于 node_modules/typescript/lib/lib.es2015.symbol.wellknown.d.ts .

    VS Code Go to Definition screenshot

    我意识到 JSON.parse JSON.stringify 方法,但是否有正当理由对任何属性使用此类型?

    0 回复  |  直到 4 年前
    推荐文章