Coder Social home page Coder Social logo

TypeScript相关 about blog HOT 2 OPEN

buddywang avatar buddywang commented on July 24, 2024
TypeScript相关

from blog.

Comments (2)

buddywang avatar buddywang commented on July 24, 2024

快捷外部模块声明

当你使用一个新模块时,如果不想要花费时间书写一个声明时,现在你可以使用快捷声明以便以快速开始。

declare module "hot-new-module";

现在你可以导入 hot-new-module 使用了,所有从快捷模块的导入都具有 any 类型。

import x, {y} from "hot-new-module";
x(y);

模块名称中的通配符

ts 不支持导入非 js 资源,要导入图片,json等资源要借助 webpack 等的 loader,那在 ts 里为了不报错要为这类模块定义一个外部模块声明。
TypeScript 2.0支持使用通配符符号(*)定义一类模块名称。这种方式,一个声明只需要一次扩展名,而不再是每一个资源。

declare module "*.json";
declare module "*.png";
...

现在,你可以导入非 js 资源了

import demoPng from './demo.png'

from blog.

buddywang avatar buddywang commented on July 24, 2024

keyof 索引类型查询操作符

keyof T, 对于任何类型 T, keyof T的结果为 T上已知的公共属性名的联合(动态的联合),例如:

interface Person {
  name: string;
  age: number;
}

let key: keyof Person // 'name' | 'age'

常见用法

  • 解决用 [key] 访问对象属性时报错
interface Person {
  name: string;
  age: number;
}

let p: Person = {
  name: 'ss',
  age: 33
}
let key = 'name'
// p[key] = 'dd' // Error: Element implicitly has an 'any' type because type 'Person' has no index signature.
p[key as keyof Person] = 'dd'

from blog.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.