Skip to content

Conversation

menthol
Copy link

@menthol menthol commented Jun 28, 2025

This PR introduces a new attribute LiteralTypeScriptExtraTypes that allows adding additional literal type information to TypeScript types. This is useful when working with specifications like JSON:API where you need to add fixed properties to your TypeScript types.

Features

  • New LiteralTypeScriptExtraTypes attribute that accepts an array of key-value pairs
  • Each key-value pair is added as a property to the generated TypeScript type
  • Values are inserted as literal TypeScript code, allowing for complex type definitions

Examples

Basic usage with a string literal:

#[TypeScript]
#[LiteralTypeScriptExtraTypes([
    'type' => "'users'",
])]
class UserData {
    public string $name;
}

Generates:

export type UserData = {
    type: 'users';
    name: string;
};

Multiple properties with complex types:

#[TypeScript]
#[LiteralTypeScriptExtraTypes([
    'type' => "'users'",
    'links' => '{self: string}',
])]
class UserData {
    public string $name;
}

Generates:

export type UserData = {
    type: 'users';
    links: {self: string};
    name: string;
};

@menthol
Copy link
Author

menthol commented Jun 28, 2025

During development, I have considered several alternative names for this attribute:

  • LiteralExtraTypeScriptTypes
  • TypeScriptExtraTypes
  • AdditionalTypeScriptTypes
  • TypeScriptExtra (and the overwriting of the transformExtra function)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant