Add a new helper type `Primitive` for JSON

未关闭
#35,185 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
30/100
Issue 类型
功能
描述清晰度
基本清楚
活跃度
停滞
技术栈
typescript
领域
compilers

调研方向

没有指定任何仓库文件、测试或入口点。首先审查与递归条件类型和映射类型相关的 TypeScript type-checker 设计,然后确定所提议的 Primitive helper 是否具有获认可的范围和可测试的语义。

由索引模型根据 Issue 内容生成。

描述

Awaiting More Feedback Suggestion

Search Terms

  • Primitive
  • JSON

Suggestion

When you encode an object to a JSON-string and decode the JSON-string to an object again, the object would be primitive. All of the methods defined in the object would be removed and its prototype would also be the primitive Object.

If the target object has a toJSON() method, it would be converted to a pritimive of returned value from the toJSON() method.

To express those conversions, I suggest a new helper type Primitive. I believe that providing the helper type Primitive is much better choice rather than hard-coding; enforcing programmers to implement duplicated (primitive) type definition, by themselves, considering the JSON encoding & decoding.

Below is the implementation code:

/**
 * Primitify a type.
 * 
 * If target type is an object, all methods defined in the object would be 
 * removed. Also, if the target type has a `toJSON()` method, its return type 
 * would be chosen.
 * 
 * @typeParam T A type to be primitive
 */
export type Primitive<Instance> = value_of<Instance> extends object
    ? Instance extends object
        ? Instance extends IJsonable<infer Raw>
            ? value_of<Raw> extends object
                ? Raw extends object
                    ? PrimitiveObject<Raw> // object would be primitified
                    : never // cannot be
                : value_of<Raw> // atomic value
            : PrimitiveObject<Instance> // object would be primitified
        : never // cannot be
    : value_of<Instance>;

/**
 * @hidden
 */
type PrimitiveObject<Instance extends object> =
{
    [P in keyof Instance]: Instance[P] extends Function
        ? never
        : Primitive<Instance[P]>
};

/**
 * @hidden
 */
type value_of<Instance> = 
    is_value_of<Instance, Boolean> extends true ? boolean
    : is_value_of<Instance, Number> extends true ? number
    : is_value_of<Instance, String> extends true ? string
    : Instance;

/**
 * @hidden
 */
type is_value_of<Instance, Object extends IValueOf<any>> = 
    Instance extends Object
        ? Object extends IValueOf<infer Primitive>
            ? Instance extends Primitive
                ? false
                : true // not Primitive, but Object
            : false // cannot be
        : false;

/**
 * @hidden
 */
interface IValueOf<T>
{
    valueOf(): T;
}

Use Cases

interface IMember
{
    id: Number;
    name: string;
    email: String;
    mobile: string;
    login_histories: LoginHistory[];
}

declare class Member
{
    private id_: Number;
    private name_: string;
    private email_: String;
    private mobile_: string;
    private login_histories_: LoginHistory[];

    public activate(password: string): boolean;
    public isActivated(): boolean;
    public toJSON(): IMember;
}

declare class LoginHistory
{
    public readonly timestamp: Date;
    public readonly success: Boolean;
}

function main(json: string): void
{
    // Member -> Primitive<ReturnType<Member.prototype.toJSON>>
    let members: Primitive<Member[]> = JSON.parse(json);
    
    // Date -> string throw Date.toJSON()
    console.log(members[0].login_histories[0].timestamp);
}
type Primitive<Member[]> = Array<{
    id: number; // Number -> number
    name: string;
    email: string; // String -> string
    mobile: string;
    login_histories: Array<{
        timestamp: string; // Date.toJSON(): string
        success: boolean; // Boolean -> boolean
    }>;
}>;

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.
主要语言
Go
星标
111k
派生
14.4k
平均合并
1 天 19 小时
30 天内合并 PR
117

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

microsoft/TypeScript 的其他 Issue

查看 microsoft/TypeScript 的全部 Issue

相似的 Issue

更多 Go Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。