Skip to content

Forbid native TypeScript collection types#5598

Merged
pquentin merged 4 commits intomainfrom
remove_native_ts_collection_types
Nov 7, 2025
Merged

Forbid native TypeScript collection types#5598
pquentin merged 4 commits intomainfrom
remove_native_ts_collection_types

Conversation

@margaretjgu
Copy link
Member

Closes #4538

@github-actions
Copy link
Contributor

Following you can find the validation changes against the target branch for the APIs.

No changes detected.

You can validate these APIs yourself by using the make validate target.

`type MyReq = Required<string>`,
`type MyPick Pick<integer,"something">`,
`type MyOmit = Omit<Record, "something">`,
`type MyDict = Dictionary<string, object>`,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original test file had a bug...it included native types like Record, Partial, etc. in BOTH the valid and invalid arrays. This was contradictory and causing test failures.

I corrected the tests to properly reflect the rule's intent:

  • valid: Now uses spec-defined aliases (Dictionary) and allowed types (arrays, custom classes)
  • invalid: Contains native utility types (Record, Partial, etc.) and collection types (Map, Set, etc.)

also added test coverage for the new collection types (Map, Set, WeakMap, WeakSet).

Copy link
Member

@pquentin pquentin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! LGTM.

Copy link
Member

@pquentin pquentin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I realize I approved, but I think the code could be made a bit more maintainable. The Array suggestion can wait, but we should create an issue for it if we don't do it today.

const TYPES_TO_AVOID = ['Record', 'Partial', 'Required', 'Pick', 'Omit'];
const UTILITY_TYPES = ['Record', 'Partial', 'Required', 'Pick', 'Omit'];

const COLLECTION_TYPES = ['Map', 'Set', 'WeakMap', 'WeakSet'];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we please add Array to the list? It could also be a follow-up, as it generates 106 errors.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will do this in a follow up pr

Comment on lines +25 to +35
const COLLECTION_TYPES = ['Map', 'Set', 'WeakMap', 'WeakSet'];

const TYPES_TO_AVOID = [...UTILITY_TYPES, ...COLLECTION_TYPES];

const TYPE_SUGGESTIONS = {
'Record': 'Use Dictionary instead',
'Map': 'Use Dictionary instead',
'Set': 'Use an array type instead (e.g., string[])',
'WeakMap': 'Use Dictionary instead',
'WeakSet': 'Use an array type instead',
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When trying to add Array to the list, I added it to TYPE_SUGGESTIONS and realized it had no effect. Can we please stop using TYPES_TO_AVOID and TYPE_SUGGESTIONS and only rely on TYPE_SUGGESTIONS? Perhaps that will enable us to provide more helpful suggestions than "Use spec-defined aliases instead". (There's no equivalent to Omit or Pick, as far as I know.)

@margaretjgu margaretjgu requested review from a team as code owners November 7, 2025 15:56
@margaretjgu margaretjgu force-pushed the remove_native_ts_collection_types branch 2 times, most recently from 141fb95 to 1b0da8f Compare November 7, 2025 16:11
Copy link
Member

@pquentin pquentin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! LGTM.

@pquentin pquentin merged commit 29fdfe0 into main Nov 7, 2025
10 checks passed
@pquentin pquentin deleted the remove_native_ts_collection_types branch November 7, 2025 18:05
github-actions bot pushed a commit that referenced this pull request Nov 7, 2025
* remove native collection types

* fix rule

* remove array type

---------

Co-authored-by: Quentin Pradet <quentin.pradet@elastic.co>
(cherry picked from commit 29fdfe0)
github-actions bot pushed a commit that referenced this pull request Nov 7, 2025
* remove native collection types

* fix rule

* remove array type

---------

Co-authored-by: Quentin Pradet <quentin.pradet@elastic.co>
(cherry picked from commit 29fdfe0)
margaretjgu added a commit that referenced this pull request Nov 7, 2025
* remove native collection types

* fix rule

* remove array type

---------


(cherry picked from commit 29fdfe0)

Co-authored-by: margaretjgu <136839162+margaretjgu@users.noreply.github.com>
Co-authored-by: Quentin Pradet <quentin.pradet@elastic.co>
margaretjgu added a commit that referenced this pull request Nov 7, 2025
* remove native collection types

* fix rule

* remove array type

---------


(cherry picked from commit 29fdfe0)

Co-authored-by: margaretjgu <136839162+margaretjgu@users.noreply.github.com>
Co-authored-by: Quentin Pradet <quentin.pradet@elastic.co>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[validation] Native typescript collection types should not be used

2 participants