首页 > 其他分享 >[Typescript] Importing and Typing non-code files in Typescript

[Typescript] Importing and Typing non-code files in Typescript

时间:2024-08-08 19:28:55浏览次数:7  
标签:files non Typescript ts squiggly under import png

Here we're attempting to import several PNG files into our TypeScript program:

import pngUrl1 from "./example1.png"; // red squiggly line under "./example1.png"
import pngUrl2 from "./example2.png"; // red squiggly line under "./example2.png"
import pngUrl3 from "./example3.png"; // red squiggly line under "./example3.png"
import pngUrl4 from "./example4.png"; // red squiggly line under "./example4.png"

 

Let's start by creating a png.d.ts file in the src directory since we need to put something in the global scope.

// inside src/png.d.ts
declare module "*.png" {
  const png: string;

  export default png;
}

Now in our index.ts file, the imported PNG images work without error. Thanks to our png.d.ts declaration, TypeScript recognizes that these imports are strings.

The key takeaway from this lesson is the versatility and potential of wildcards in module declarations. This is a handy technique to use when working with non-JavaScript files in a TypeScript project.

标签:files,non,Typescript,ts,squiggly,under,import,png
From: https://www.cnblogs.com/Answer1215/p/18349570

相关文章

  • [Typescript] The declare module Syntax in TypeScript
    Herewe'reimportingafunction myModuleFunc from my-module:import{myModuleFunc}from"my-module";//redsquigglylineunder"my-module" Let'sstartbycreatinganewdeclarationfilecalled my-module.d.ts insideofth......
  • [Typescript] tsconfig libs and target
    Intsconfigfile,youhave targetand libsconfiguration.Youalwaysneedtodefinea target,recommendedas es2022Specifyingthe lib optionalsoletsusdrilldownintothespecificfeaturesandlibrarieswewanttoincludeinourproject,whichwewill......
  • [Typescript] Using Variables Declared Elsewhere
    The declare keywordinTypeScriptallowsyoutospecifytypesforglobalvariables.Wheneveryouuseit,anambientcontextiscreated,whichmeansthatthevariableisinjectedwithoutneedingtoprovideanimplementation.Here'showwewoulduse de......
  • [Typescript] Annotating the Errors Thrown by a Function
    /***Howdoweannotatetheerrorsthisfunctionthrows?*/typePossibleErrors=SyntaxError|DOMException;constgetUserFromLocalStorage=(id:string)=>{constuser=localStorage.getItem(id);if(!user){returnundefined;}re......
  • [Typescript] Type incompatible function argument as never
    Forthefollowingcode:constobjOfFunctions={string:(input:string)=>input.toUpperCase(),number:(input:number)=>input.toFixed(2),boolean:(input:boolean)=>(input?"true":"false"),};constformat=(i......
  • [Typescript] Typing Functions with Object Params
    import{expect,it,vitest}from'vitest';constlogId=(obj:{id:string})=>{console.log(obj.id);};constlogName=(obj:{name:string})=>{console.log(obj.name);};constloggers=[logId,logName];constlogAll=(o......
  • [Typescript] Understanding TypeScript's Function Parameter Comparisons
    Makethosepass:import{Equal,Expect}from"@total-typescript/helpers";typeEvent="click"|"hover"|"scroll";typeCallbackType=unknown;constlistenToEvent=(callback:CallbackType)=>{};listen......
  • 【TS】 TypeScript声明文件:打通JavaScript和TypeScript的桥梁
     TypeScript声明文件的讲解: TypeScript声明文件(DeclarationFile)在TypeScript项目中具有举足轻重的地位,它是连接TypeScript严格的类型系统与外部无类型或类型不明确的JavaScript代码的关键纽带。 声明文件的核心价值在于为TypeScript编译器提供必要的类型信息......
  • 解决Python的pip问题:WARNING: Retrying (Retry(total=1, connect=None, read=None, re
    相关:pip安装第三方库报错Retrying(Retry(total=1,connect=None,read=None,redirect=None,status=None))国内镜像源下载常用国内源:清华:https://pypi.tuna.tsinghua.edu.cn/simple/阿里云:http://mirrors.aliyun.com/pypi/simple/中国科技大学https://pypi.mirrors.u......
  • 服务异常,报too many open files
    "toomanyopenfiles"错误表示进程打开的文件句柄数量超出了操作系统允许的最大限制。解决方法:临时增加限制:可以使用命令 ulimit-n<数量> 来临时提升当前shell会话中的打开文件数量限制。永久增加限制:编辑 /etc/security/limits.conf 文件,添加或修改相应的行来......