Skip to content

Commit

Permalink
Generate branded primitives instead of unknown type for opaque types …
Browse files Browse the repository at this point in the history
…without super type and type parameters

Summary:
Changelog:
[Internal] - Added generation of branded primitives instead of unknown type for opaque types without super type and type parameters

Reviewed By: pieterv

Differential Revision: D70564934

fbshipit-source-id: b2d302219141f5175777b419fb577dd6a031008c
  • Loading branch information
coado authored and facebook-github-bot committed Mar 5, 2025
1 parent 7f86010 commit 2199086
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ exports[`flowDefToTSDef opaque-type/declare 1`] = `
* @format
*/

declare type T = unknown;
declare type T = symbol & {__T__: string};
"
`;
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ exports[`flowDefToTSDef opaque-type 1`] = `
* @format
*/

declare type T = unknown;
declare type T = symbol & {__T__: string};
"
`;
44 changes: 44 additions & 0 deletions tools/hermes-parser/js/flow-api-translator/src/flowDefToTSDef.js
Original file line number Diff line number Diff line change
Expand Up @@ -1709,6 +1709,50 @@ const getTransforms = (
// TODO - we could simulate this in a variety of ways
// Examples - https://basarat.gitbook.io/typescript/main-1/nominaltyping

if (node.supertype == null && node.typeParameters == null) {
const name = `__${node.id.name}__`;
return {
type: 'TSTypeAliasDeclaration',
loc: DUMMY_LOC,
declare: true,
id: transform.Identifier(node.id, false),
typeAnnotation: {
type: 'TSIntersectionType',
types: [
{
type: 'TSSymbolKeyword',
loc: DUMMY_LOC,
},
{
type: 'TSTypeLiteral',
loc: DUMMY_LOC,
members: [
{
type: 'TSPropertySignature',
computed: false,
loc: DUMMY_LOC,
key: {
type: 'Identifier',
name: name,
loc: DUMMY_LOC,
},
typeAnnotation: {
type: 'TSTypeAnnotation',
loc: DUMMY_LOC,
typeAnnotation: {
type: 'TSStringKeyword',
loc: DUMMY_LOC,
},
},
},
],
},
],
loc: DUMMY_LOC,
},
};
}

return {
type: 'TSTypeAliasDeclaration',
loc: DUMMY_LOC,
Expand Down

0 comments on commit 2199086

Please sign in to comment.