Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I export default with type?

Tags:

typescript

How can I rewrite this so that I don't have the const?

const theme: ITheme = { color: 'blue' };
export default theme;

The following will disable type checks which is not what I want.

export default  {color: Blue} as ITheme

May be related

  • export { default as x } does not infer type but diffent usage.
like image 491
Archimedes Trajano Avatar asked Apr 28 '26 02:04

Archimedes Trajano


1 Answers

For TypeScript 4.9+ you can use the satisfies keyword to ensure the type is statically checked:

export default { color: 'blue' } satisfies ITheme;

This will guarantee the type is valid, but may set a more specific type ({ color: 'blue' } instead of ITheme). If you need the more generic type, use satisfies and as:

export default { color: 'blue' } satisfies ITheme as ITheme;
like image 51
rmobis Avatar answered Apr 29 '26 20:04

rmobis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!