[Typescript] Only Type import or export

import type only imports declarations to be used for type annotations and declarations. It always gets fully erased, so there’s no remnant of it at runtime. Similarly, export type only provides an export that can be used for type contexts, and is also erased from TypeScript’s output.

import type { SomeThing } from "./some-module.js";
export type { SomeThing };

 

// actual code with JSDoc comments
/* eslint-disable promise/always-return */
import { useEffect } from 'react';
import Deferred from './deferred';

/**
 * 
 * @param {() => Promise} getData 
 * @param {{
    stateName: string;
    otherStatesToMonitor?: unknown[];
    setter: (arg: x) => void;
  }} options 
  @return {void}
 */
export function useAsyncDataEffect(getData, options) {
  let cancelled = false;
  const { setter, stateName } = options;
  useEffect(() => {
    const d = new Deferred();

    getData()
      .then((jsonData) => {
        if (cancelled) return;
        else d.resolve(jsonData);
      })
      .catch(d.reject);

    d.promise
      .then((data) => {
        if (!cancelled) {
          console.info(
            '%c Updating state: ' + stateName,
            'background: green; color: white; display: block;',
          );
          setter(data);
        }
      })
      .catch(console.error);
    return () => {
      cancelled = true;
    };
  }, [...(options.otherStatesToMonitor || []), stateName]);
}

 

We can import the type only:

import type {useAsyncDataEffect} from "./src/utils/api"

But we cannot use type as value:

useAsyncDataEffect() // Error: 'useAsyncDataEffect' cannot be used as a value because it was imported using 'import type'.

 

More doc

posted @   Zhentiw  阅读(83)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2021-08-24 [Cloud Architect] 1. Design for Availability, Reliability, and Resiliency
2020-08-24 [CSS] Use grid-template to make your CSS Grid declarations more readable
2020-08-24 [Machine Learning Ex2] Linear Regression with Multiple Variables
2020-08-24 [Machine Learning] Normal Equation Noninvertibility
2020-08-24 [Machine Learning] Normal Equation for linear regression
2017-08-24 [D3] Animate with the General Update Pattern in D3 v4
2016-08-24 [Ramada] Build a Functional Pipeline with Ramda.js
点击右上角即可分享
微信分享提示