Skip to content

Add LiteralTypeScriptExtraTypes attribute for enhanced TypeScript type definitions #107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

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