Comments (2)
快捷外部模块声明
当你使用一个新模块时,如果不想要花费时间书写一个声明时,现在你可以使用快捷声明以便以快速开始。
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.
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)
- DOMContentLoaded 与 load、defer 与 async
- vue-cli使用相关 HOT 2
- webpack相关
- Node相关
- Vue styleguidist相关
- standard-version:版本控制、生成 changelog、打 tag
- element 和 mouseEvent 的位置和尺寸信息相关
- 用class+ts编写vue组件:vue-property-decorator相关
- 尾调用优化:尾递归优化
- 严格模式、替代 eval 的 Function
- TypeScript的模块解析逻辑(类似Node)
- 全屏H5时兼容刘海屏
- css坑!height 使用百分比单位时注意
- in 和 hasOwnProperty
- MutationObserver:监听DOM变化事件(插入、删除、变更等)的观测器
- eject啥意思
- babel插件
- node笔记-全局对象和全局变量
- 动画的12项基本法则
- Git-flow
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from blog.