Hone logo
Hone
Problems

Type-Only Imports in TypeScript: Modularizing Type Definitions

TypeScript's type-only imports allow you to bring in type definitions from modules without actually importing any code. This is incredibly useful for modularizing your type definitions, preventing runtime dependencies, and improving code clarity. This challenge asks you to implement a function that validates whether a given import statement is a type-only import.

Problem Description

You are tasked with creating a function isTypeOnlyImport that takes a TypeScript import statement string as input and returns true if the import is a type-only import, and false otherwise. A type-only import is indicated by the presence of the type keyword in the import statement.

What needs to be achieved:

  • The function should accurately identify type-only import statements.
  • The function should handle various valid import statement formats.
  • The function should return a boolean value indicating whether the import is type-only.

Key requirements:

  • The function must accept a string representing a TypeScript import statement.
  • The function must return true if the import statement includes the type keyword.
  • The function must return false if the import statement does not include the type keyword.
  • The function should be case-sensitive regarding the type keyword.

Expected behavior:

The function should correctly identify type-only imports regardless of the specific module being imported or the syntax used. It should also correctly identify non-type-only imports.

Edge cases to consider:

  • Empty import statements.
  • Import statements with comments.
  • Import statements with multiple modules.
  • Import statements with aliasing.
  • Import statements with different syntaxes (e.g., import type { MyType } from 'module'; vs. import { MyType as MyAlias } from 'module';).
  • Import statements with default imports.

Examples

Example 1:

Input: "import type { MyType } from 'module';"
Output: true
Explanation: The import statement explicitly uses the `type` keyword.

Example 2:

Input: "import { MyType } from 'module';"
Output: false
Explanation: The import statement does not use the `type` keyword.

Example 3:

Input: "import { MyType as Alias } from 'module';"
Output: false
Explanation:  Even with aliasing, the absence of `type` makes it a regular import.

Example 4:

Input: "import type ModuleName from 'module';"
Output: true
Explanation: Importing a module as a type is also a type-only import.

Example 5:

Input: "import * as ModuleName from 'module';"
Output: false
Explanation: Importing everything as a namespace is not a type-only import.

Constraints

  • The input string will always be a valid TypeScript import statement string.
  • The function must have a time complexity of O(n), where n is the length of the input string. While a simple string search is sufficient, avoid overly complex regular expressions that could degrade performance.
  • The function must be written in TypeScript.

Notes

  • Consider using string methods like includes() or indexOf() for efficient searching.
  • The focus is on identifying the presence of the type keyword, not on parsing the entire import statement.
  • Be mindful of case sensitivity when searching for the type keyword.
  • Think about how to handle different import syntaxes gracefully. A simple check for the presence of "type" is sufficient.
Loading editor...
typescript