You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Reacted by Nacho Castillo, Cristi, Ville Immonen, Raphael, Vytenis, Igor Krupenja, Francis Niu 牛飞龙, Luca Trazzi, Ashton Harris, Yassine Zeriouh and 7 more
Reacted by inker, Trevor Jex, Keith Tan, Jonathan MASSUCHETTI, Jiji Freya Daniel Maslowski, Mathieu TUDISCO, Boris Yankov, Adam Arthur, Rohit Gohri, Marko Schulz and 169 more
Reacted by Nikolay Kudryavtsev, ljmsu, ravshansbox, TheBlckbird, Elijah Mock, Vincent Kelleher, Kutner JS and Paul Maréchal
Yes, that's what I do know but I wish I didn't have to.
Reacted by Keegan Farley, inker, Brian Bugh, Scott Bedard, X.L, Keith Tan, Jonathan MASSUCHETTI, Calvin Woo, Aaron J. Lang, Jiji Freya Daniel Maslowski and 195 more
I think it's a little more related to to the following scenario regarding function declarations.
if I want to write a decorator that is verified against the PropertyDecorator type in lib.d.ts, I can't write it easily. I have to use a function expression.
TypeError: controller.default is not a function at fs_1.readdirSync.filter.forEach.Promise.resolve.then.then.controller (/Users/bbendavi/Documents/cdt-box/dist/server/config/routes.js:16:71) at <anonymous> at process._tickCallback (internal/process/next_tick.js:160:7) at Function.Module.runMain (module.js:703:11) at startup (bootstrap_node.js:193:16) at bootstrap_node.js:617:3
import * as webpack from 'webpack'
export default {
...
} as webpack.Configuration
Reacted by Aron Jones, ADoyle, Zach Auclair, Jean-Baptiste Louazel, bykbtzr, clem, Brian M Hunt, gepd, Mikael Lirbank, Alexander Kim and 33 more
Reacted by Roman, Tom Crockett, Enteleform, Ben Coveney, CertainPerformance, Sheikh Abdul Wahid Ahmed, Oleh Misarosh, Gareth Jones, Vladas Mikalajūnas, Artha and 108 more
Reacted by David Velasco, ૮༼⚆︿⚆༽つ, Wes Roberts and Felipe Lincoln
Reacted by Aron Jones, Emmanuel Di Iorio, A, Leandro Cotti and Predrag Stojadinović
Reacted by Felipe Lincoln, kooskoos20 and Sushant Kumar
is that it's a cast, so it purposely loses type safety.
Reacted by Keegan Farley, Christopher Carman, Ben Coveney, CertainPerformance, arianrhodsandlot, Inirit, Aaron Tribou, Ashlynne Mitchell, codelovesme, Mark Penner and 221 more
Tom Crockett (@pelotom) As a general matter of fact, you're completely right, I'd avoid putting this in my codebase. For the scope of this thread where this is about webpack configuration, I think we're just fine
Jean-Baptiste Louazel (@jlouazel) leaving aside whether type safety is any less important in a webpack config... the issue is not specific to webpack, it's about annotating the type of the default export in general.
Reacted by Denis Mukhametov, Inirit, Maxim Golovanev, Brian M Hunt, Mark Elphinstone, Francesc Rosas, Bart Lens, Nikolay Kudryavtsev, Lucas Forster, P. Envall and 9 more
Just want to point out that using both as and satisfies might actually be the best solution here until we get actual syntax. satisfies checks that the value conforms to the type, as forces the type instead of allowing TS infer its own type
typeUnion={v: 1|2}exportdefault{v: 1}satisfiesUnionasUnion;// Checked against Union and Union is preserved as the type of the export.
satisfies is a type assertion. It does not type the variable
Oh, too bad. I thought an earlier commenter said that it did type it. We haven't upgraded to the version of typescript with this yet, but this was one thing I was looking forward to.
For the foreseeable future, we'll be working off the "most-upvoted" suggestion list. Please don't post comments in issues or iteration plans asking for feature updates. Once the top suggestions have been cleared out, we might change this policy, possibly by having a survey or something.
I don't see anything indicating that has changed, but it's possible I missed it
With that being said, if this issue is considered for a Feature Update, whoever is considering it, please note that #56235 (comment) (export default ... satisfies as T) is a possible solution to this issue and many others
Reacted by Mr. Binary Sniper, Mark Penner, bgenia, Jose Vargas and Azis Ramadhan
Activity
mhegazy commentedon Jan 23, 2017
Would not this be sufficient?
mohsen1 commentedon Jan 23, 2017
Yes, that's what I do know but I wish I didn't have to.
mhegazy commentedon Jan 23, 2017
related to #3792. we have tried to keep the module export as simple as possible on the syntax side.
DanielRosenwasser commentedon Jan 24, 2017
I think it's a little more related to to the following scenario regarding function declarations.
if I want to write a decorator that is verified against the
PropertyDecoratortype inlib.d.ts, I can't write it easily. I have to use a function expression.which is case of friction whenever I want to actually implement a decorator without making a mistake.
barakbd commentedon Feb 20, 2018
I am trying to use a default export in the following way
I get an error:
TypeError: controller.default is not a function at fs_1.readdirSync.filter.forEach.Promise.resolve.then.then.controller (/Users/bbendavi/Documents/cdt-box/dist/server/config/routes.js:16:71) at <anonymous> at process._tickCallback (internal/process/next_tick.js:160:7) at Function.Module.runMain (module.js:703:11) at startup (bootstrap_node.js:193:16) at bootstrap_node.js:617:3I asked this on SO with a full file examples:
https://stackoverflow.com/questions/48696327/how-to-define-a-module-default-export-type-in-typescript
Would much appreciate your help!
Igor-Gold commentedon Mar 10, 2019
jlouazel commentedon Apr 1, 2019
@IgorGee That's the proper way of doing it IMHO
pelotom commentedon Apr 1, 2019
The problem with
is that it's a cast, so it purposely loses type safety.
jlouazel commentedon Apr 1, 2019
Tom Crockett (@pelotom) As a general matter of fact, you're completely right, I'd avoid putting this in my codebase. For the scope of this thread where this is about webpack configuration, I think we're just fine
pelotom commentedon Apr 1, 2019
Jean-Baptiste Louazel (@jlouazel) leaving aside whether type safety is any less important in a webpack config... the issue is not specific to webpack, it's about annotating the type of the default export in general.
oli-laban commentedon Jun 10, 2019
@IgorGee That's forbidden in
@typescript-eslint/recommended.28 remaining items
dobesv commentedon Jan 31, 2023
I think that
satisfiesthis request to my satisfaction, in that case.mon-jai commentedon Jan 31, 2023
Siddhaarth Verma (@sid) Vishnoi It works on function declarations too.
Playground Link
dragomirtitian commentedon Mar 16, 2023
Just want to point out that using both
asandsatisfiesmight actually be the best solution here until we get actual syntax.satisfieschecks that the value conforms to the type,asforces the type instead of allowing TS infer its own typePlayground Link
Thanks to Ashley Claymore (@acutmore) for suggesting this use of
satisfies T as Trampion commentedon Jun 30, 2023
Another workaround is to pass the export value to an identity function to check its value:
I'm not sure if there's a canonical identity function that'd avoid introducing your own.
ekcom commentedon Jul 25, 2023
For now, we continue to
export default exportDefault;with something likeconst exportDefault: webpack.Configuration = {};.asinduces type coercionsatisfiesis a type assertion. It does not type the variable (even though it will validate it)dobesv commentedon Jul 25, 2023
Oh, too bad. I thought an earlier commenter said that it did type it. We haven't upgraded to the version of typescript with this yet, but this was one thing I was looking forward to.
mccallofthewild commentedon Oct 4, 2023
You can also declare a typed variable and assign it in your export.
mon-jai commentedon Oct 4, 2023
McCall Alexander (@mccallofthewild) Then it could be reassigned.
mccallofthewild commentedon Oct 4, 2023
Mon (@mon-jai) No. 😁 That's not how assignment works.
mon-jai commentedon Oct 5, 2023
Shouldn't the following code work?
MacroMan commentedon Dec 18, 2023
satisfies T as Tdoesn't reduce boilerplate in all circumstances. eg using a type from a value doesn't work at all:and even if it did work, I'm repeating my type declaration twice.
Doing
export default: SomeType<typeof SomeValue> {some:'value'}would be great for reducing boilerplateMrBns commentedon Sep 2, 2024
from 2017 -> 2024... Still this issue is open. wow.
a-tarasyuk commentedon Sep 2, 2024
Mr. Binary Sniper (@MrBns) Some time ago, Ryan Cavanaugh (@RyanCavanaugh) proposed an initiative to deal with persistent issues - #54923.
You might want to ask Ryan Cavanaugh (@RyanCavanaugh) to add this issue to the list for upcoming discussions.saltman424 commentedon Sep 2, 2024
Oleksandr Tarasiuk (@a-tarasyuk) unfortunately, in the announcement of that initiative they include:
I don't see anything indicating that has changed, but it's possible I missed it
With that being said, if this issue is considered for a Feature Update, whoever is considering it, please note that #56235 (comment) (
export default ... satisfies as T) is a possible solution to this issue and many otherschbybnwr commentedon Feb 25, 2025
For maximum type safety:
If you don't hate micro package:
@txe/define-x