Skip to content

Conversation

@Baptiste-hero
Copy link

Convert TransformableInfo from interface to generic type with TCustom parameter, allowing custom properties to be properly typed. Update printf function to support the generic parameter for better type inference when using custom fields.

Convert TransformableInfo from interface to generic type with TCustom
parameter, allowing custom properties to be properly typed. Update
printf function to support the generic parameter for better type
inference when using custom fields.
@Baptiste-hero
Copy link
Author

Add Generic Support to TransformableInfo for Type-Safe Custom Properties

Problem:

The printf function accepts a callback with a TransformableInfo parameter. While TransformableInfo allows arbitrary keys via [key: string | symbol]: unknown, these custom properties are not properly typed, forcing users to rely on type assertions.

Current Workaround:

format.printf((info: TransformableInfo) => {
  const { level, message, additionalData } = info as TransformableInfo & { 
    additionalData: Record<string, unknown> 
  };
});

Proposed Solution:

With this change, users can specify custom property types directly:

format.printf((info: TransformableInfo<{ additionalData: Record<string, unknown> }>) => {
  const { level, message, additionalData } = info; // Fully typed!
});

Benefits:

  • Enhanced type safety for custom log properties
  • Better IDE autocomplete and IntelliSense
  • Eliminates need for manual type assertions
  • Fully backward compatible (default generic parameter {})

Implementation Notes:

This change converts TransformableInfo from an interface to a type alias using intersection types to support generic type parameters. While this is technically a breaking change (classes can no longer extends TransformableInfo, and declaration merging is no longer possible), the practical impact should be minimal as TransformableInfo is primarily used as a type annotation rather than extended or merged.

I'm open to discussion about this implementation, as I don't have full context on logform's architecture and usage patterns. If the interface-to-type conversion is problematic, I'd be happy to explore alternative approaches.

Thank you for considering this contribution!

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