This is a type alias - it's used to give another name to a type.
(Compare type vs interface here)
In your example, feline will be the type of whatever cat is.
Here's a more full fledged example:
interface Animal {
legs: number;
}
const cat: Animal = { legs: 4 };
export type feline = typeof cat;
feline will be the type Animal, and you can use it as a type wherever you like.
const someFunc = (cat: feline) => {
doSomething(cat.legs);
};
export simply exports it from the file. It's the same as doing this:
type feline = typeof cat;
export {
feline
};
Answer from James Monger on Stack Overflow[request] allow `export type * from`
`export type * from 'somewhere'`
What does export type mean in TypeScript? - Ask a Question - TestMu AI Community
TypeScript export imported interface - Stack Overflow
Videos
Hello,
I've been putting types that can be used all over my app on types.ts files. The interfaces and types in these files don't get exported and this way VSCode / Typescript somehow knows that they exist globally. This avoids having to import them whenever they are used as normally I already have quite a few other imports for actual code. That's the main reason I've been using to not having to explicitly export/import those types.
However recently I came up with two other thoughts:
-
When creating a library, it seems the way to go is to export types. This way you can select which types are exposed (or not) and you avoid "namespacing". To avoid name conflicts when not explicitly exporting types, I normally use something like
PossiblyLongNamePropsorPossiblyLongNameState. These names can get long and repetitive (thePossiblyLongNamepart). -
These long names for "namespacing" got me thinking that I should probably be exporting types, at least in some cases. So it's sort of a conflict between "I don't want to be using long names for my interfaces/types" vs "I don't want to explicitly import every single type I use".
Does that make sense? What do you think? What's your approach to this?
Thank you!
There is an export import syntax for legacy modules, and a standard export format for modern ES6 modules:
// export the default export of a legacy (`export =`) module
export import MessageBase = require('./message-base');
// export the default export of a modern (`export default`) module
export { default as MessageBase } from './message-base';
// when '--isolatedModules' flag is provided it requires using 'export type'.
export type { default as MessageBase } from './message-base';
// export an interface from a legacy module
import Types = require('./message-types');
export type IMessage = Types.IMessage;
// export an interface from a modern module
export { IMessage } from './message-types';
Some more examples besides #c-snover's answer from here. You can put them together.
import 'jquery'; // import a module without any import bindings
import $ from 'jquery'; // import the default export of a module
import { $ } from 'jquery'; // import a named export of a module
import { $ as jQuery } from 'jquery'; // import a named export to a different name
import * as crypto from 'crypto'; // import an entire module instance object
export var x = 42; // export a named variable
export function foo() {}; // export a named function
export default 42; // export the default export
export default function foo() {}; // export the default export as a function
export { encrypt }; // export an existing variable
export { decrypt as dec }; // export a variable as a new name
export { encrypt as en } from 'crypto'; // export an export from another module
export * from 'crypto'; // export all exports from another module
// (except the default export)
Without declaration files you can develop a package in TypeScript, compile it and expose it to other users as JavaScript code. Including them also allows TypeScript developers to use the package with any types you defined in it. They can get more type information whilst working with your library, such as required arguments types, function return types etc, as well as warnings from their IDE/Intellisense when there are conflicts.
The declaration: true in the file tsconfig.json instructs the TypeScript compiler to output declaration files (.d.ts). Often they are bundled in a single file e.g. index.d.ts and then a "types": path/to/index.d.ts field is added to the library's package.json file to inform TypeScript where to look for the types (when a user imports the package).
For those who are like me, and have made sure declaration: true is set in your tsconfig.json, and that your build process correctly creates the corresponding .d.ts files to the appropriate directory pointed to by your package.json file, AND STILL somehow can't access the internal types of your module when testing on an external project -- try restarting the TS server in VSCode (assuming you're using VSCode)

So much time was wasted trying to figure this out, only to realize Typescript was functioning fine and I was being sabotaged by my IDE.
I'm writing a package that uses a third party package that doesn't include its own typescript definitions. I've written my own .d.ts definitions for this third party package using a format like the following:
declare module 'third-party-package' {
class ThirdParty {
// methods and whatnot here...
}
namespace ThirdParty {
type Options = {
// various options...
}
// and a few more helper types here...
}
export default ThirdParty
}The problem is, my package can accept a callback that will pass this third party class to it, so I would also like to export my definition for it as well. That way a user could do something like this:
import MyClass, { ThirdParty } from 'my-package'
const x = new MyClass({ callback: (tp: ThirdParty) => {
// do something with the third party class here...
}})Note that I do not need to export the actual third party class, just types for it. I know how to export my types, and I can define the types for this third party package internally, but I cannot seem to figure out how to do both without basically defining these types twice (one to export and one to use for the package internally). Of course I don't want to do it like this.
Anyone have any idea how to go about defining this third party package in one place and exporting this definition for consumers of my package to use? Thanks!