We can't find the internet
Attempting to reconnect
Something went wrong!
Hang in there while we get back on track
When set to true, allowSyntheticDefaultImports
allows you to write an import like:
import React from "react";
instead of:
import * as React from "react";
When the module does not explicitly specify a default export.
For example, without allowSyntheticDefaultImports
as true:
// @filename: utilFunctions.js
const getStringLength = (str) => str.length;
module.exports = {
getStringLength,
};
// @filename: index.ts
import utils from "./utilFunctions";
// Module '"/home/runner/work/TypeScript-Website/TypeScript-Website/packages/typescriptlang-org/utilFunctions"' has no default export.
const count = utils.getStringLength("Check JS");
This code raises an error because there isnβt a default
object which you can import. Even though it feels like it should. For convenience, transpilers like Babel will automatically create a default if one isnβt created. Making the module look a bit more like:
// @filename: utilFunctions.js
const getStringLength = (str) => str.length;
const allFunctions = {
getStringLength,
};
module.exports = allFunctions;
module.exports.default = allFunctions;
This flag does not affect the JavaScript emitted by TypeScript, itβs only for the type checking. This option brings the behavior of TypeScript in-line with Babel, where extra code is emitted to make using a default export of a module more ergonomic.
If this post was enjoyable or useful for you, please share it! If you have comments, questions, or feedback, you can email my personal email. To get new posts, subscribe use the RSS feed.