[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'.
分类:
TypeScript
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源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