Closed
Description
- https://github.com/justinlnx/ts-optional-ambient-types
- Note to self: more context in an email
Two low-hanging fruits for improvement:
- Reusing type annotations on optional parameters:
Declaration emit would use the parameter type
// a.ts export interface Foo {} declare global { namespace ns { export { Foo } } } // b.ts type Foo = ns.Foo; const bar = (foo?: Foo) => {};
Foo
as written if the parameter weren’t optional, and ifbar
were written as a function declaration. When it’s optional, understrictNullChecks
, it creates a new type identity unioned withundefined
and prints that new type, so it doesn’t realizeFoo
is locally accessible, and it doesn’t randomly trawl global namespaces so it would never come up withns.Foo
, so instead it writesimport("./a").Foo
.Allegedly, even withoutstrictNullChecks
, it still starts going through this route and doesn’t realize that we should just be able to print the type as written. - An existing workaround is to replace
type
withimport
. So declaration emit is looking to see if aliases are in scope, but not type aliases or other local declarations.