TypeScript Version: 2.1.4
Code
import { MyNamespace } from 'my-package';
// importing Class objects:
const { NavLink, NavGroup } = MyNamespace; // VALID
// if we want the types as well:
type { NavLink, NavGroup } = MyNamespace; // NOT VALID
// So we have to go manual:
type NavLink = MyNamespace.NavLink; // VALID
type NavGroup = MyNamespace.NavLink; // VALID
Expected behavior:
Being able to use destructuring for types same as we use for objects.
Actual behavior:
Can only use destructuring for objects.
If this is supported, it seems like it should work for the import statement as well so that one may acquire both the type and value space meanings if desired.
import { NavLink, NavGroup } = MyNamespace; // NOT VALID
Another annoying thing is the expected variable-declaration to have a typedef lint rule.
We had to suppress it since const { NavLink, NavGroup } = MyNamespace; had no type...
so import { NavLink, NavGroup } = MyNamespace; will solve that.
This would be a great feature to have. Looks like general users are struggling with this as well: http://stackoverflow.com/questions/31394725/typescript-destructuring-alias-import
An interim that might make sense on the way to type alias destructure might be support for type aliases for namespaces:
import { MyNamespace } from 'my-package';
type N = MyNamespace;
type NavLink = N.NavLink;
Logic for this is that ES supported the above assignment of variables prior to being able to destructure.
I was hitting this today. Would be fantastic.
Encountered this scenario this day.
I had been using typescript import in place of ES6 imports in order of be more explicit on what is a type a what is javascript.
Ex:
import { Foo, Bar } from './classes'; // Javascript classes
type FooType = import('./types').FooType;
type BarType = import('./types').BarType;
Would be nice to have destructuring for type imports just like ES6 imports.
Ex:
import { Foo, Bar } from './classes'; // Javascript classes
type { FooType, BarType } = import('./types');
Any Roadmap for this feature or never be?
Aliasing is problematic when the type to be imported has generics:
// ./lib.ts
export type Foo<T extends string> = { foo: T }
// ./index.ts
// ERROR
type Foo = import('./lib').Foo;
// We have to repeat generics:
type Foo<T extends string> = import('./lib').Foo<T>;
// Separately: expected error, but got none. Bug?
type Foo<T> = import('./lib').Foo<T>;
your are right!
Probably, aliasing with generics is what stop any move on this.
Would be cool to destructure tuple types as well:
type [NameType, ValueType] = Parameters<typeof methodWithNameAndValue>;
Would love a way to get around this; one practical example where I'm experiencing this is when using the output from protobuf.js's pbjs and pbts tools. They use namespaces, and you can end up with a huge list of imports at the top of the file that could be a lot more compact.
Some examples:
function foo(x: number, y: number): number {
return x + y;
}
type [X, Y] = Parameters<typeof foo>;
const x:X = 1;
const y:Y = 3;
foo(x, y);
type Foo = {
x: number;
y: number;
}
type {X, Y} = Foo;
const x:X = 1;
const y:Y = 3;
@sheetalkamat, maybe you know, is there an active discussion of similar feature?
Most helpful comment
If this is supported, it seems like it should work for the
importstatement as well so that one may acquire both the type and value space meanings if desired.