microsoft / microsoft/TypeScript

Allow interface merging for DecoratorMetadataObject and parameterization by This

オープン
#60,997 コメント 0 件 リアクション 2 件 担当者 1 名 GitHub で見る

@rbuckton がすでに取り組んでいます。

2025年2月13日 から。

Needs Investigation
主要言語
Go
スター
111k
フォーク
14.4k
平均マージ
1日 19時間
マージ済み PR(30日)
117

説明

⚙ Compilation target

ESNext

⚙ Library

ESNext

Missing / Incorrect Definition

Currently DecoratorMetadataObject is defined as a type, however as metadata objects are intended to store values on behalf of user-defined decorators it would be useful to be able to specify more precise types for properties that such decorators add as metadata.

Sample Code

As an example, at current if we do:


const jsonFields = Symbol('jsonFields');

function field<This>(field: undefined, ctx: ClassFieldDecoratorContext<This>) {
    ctx.metadata[jsonFields] ??= [];
    // Object is of type 'unknown'.(2571)
    ctx.metadata[jsonFields].push(ctx.access.get);
}

function toJSON(obj: any) {
    // Get metadata from obj.constructor and process fields here
    // ...
}

class Foo {
    @field
    x: number = 3;
    @field 
    y: number = 4;
}

Then we get a fairly expected Object is of type 'unknown'.(2571) error.

However if allow declaration merging, then we can have better typing:

declare global {
    interface DecoratorMetadataObject {
        [jsonFields]?: Array<(this: object) => any>; 
    }
}

// Define decorator as before

If we further allow the metadata context to be parameterized with This, we can even allow even better typing:

declare global {
    interface DecoratorMetadataObject<This> {
        [jsonFields]?: Array<(this: This) => any>; 
    }
}

// define decorators...

class Foo {
    @field
    x = 3;
}

Foo[Symbol.metadata] // { [key: string]: unknown; [jsonFields]?: Array<(this: Foo) => any> }

This also helps detect conflicts if two libraries try to the use the same string-based decorator field:

// lib1
interface DecoratorMetadataObject {
    meta?: number;
}

function lib1Decorator() { /* ... */ }

// lib2
interface DecoratorMetadataObject {
    // Subsequent property declarations must have the same type.
    // Property 'meta' must be of type 'number | undefined', but here has type 'string | undefined'.(2717)
    meta?: string;
}


function lib2Decorator() { /* ... */ }

(and if TS adds type based metadata, it can give those appropriate types in the metadata object in the same way).

Documentation Link

No response

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

評価

この issue はまだ評価されていません。

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。