IES 前端架构 - 基础体验 - 龙逸楠
Hover information
Hover information
Rename
Code navigation
Refactoring
Auto imports
Code suggestions
....
{
"compilerOptions": {
"strict": true,
"allowSyntheticDefaultImports": true,
"skipLibCheck": true,
"target": "ES5",
"module": "ESNext",
"importHelpers": true,
"jsx": "react",
"moduleResolution": "Node",
"baseUrl": "./src",
"paths": {
"~/*": [
"./*"
]
}
},
"include": ["./src"],
"exclude": ["node_modules"]
}
TypeScript 不处理 import 的路径,baseURL + paths 只影响 type resolution,不影响 emit 出来的代码。
You may don't need babel/babel-loader
TypeScript 也可以用来编译 JavaScript,只需要加上 allowJs 编译参数
https://github.com/ReactiveX/rxjs/pull/2093
需要手动配置
通过 core-js 可以配置出简单通用并且体积可控的 Polyfill
import 'core-js/features/promise'
import 'core-js/features/array'
import 'core-js/features/object'
import 'core-js/stage/3'
const Polyfills: Promise<void>[] = []
export function asyncPolyfill() {
if (typeof Promise === 'undefined') {
// @ts-ignore
Polyfills.push(import('core-js/features/promise'))
}
if (!Object.assign) {
// @ts-ignore
Polyfills.push(import('core-js/features/object/assign'))
}
return Promise.all(Polyfills)
}
import { render } from 'react-dom'
import { asyncPolyfill } from './async-polyfill'
asyncPolyfill().then(() => {
render(...)
})
{
"compilerOptions": {
"strict": true,
"allowSyntheticDefaultImports": true,
"skipLibCheck": true,
"target": "ES2018",
"module": "CommonJS",
"importHelpers": true,
"moduleResolution": "Node",
"baseUrl": "./src",
"paths": {
"~/*": [
"./*"
]
}
},
"include": ["./src"],
"exclude": ["node_modules"]
}
nodemon + ts-node + tsconfig-paths
{
"delay": 1000,
"restartable": "rs",
"env": {
"NODE_ENV": "development"
},
"execMap": {
"ts": "ts-node -r tsconfig-paths/register"
// node --inspect-brk -r ts-node/register -r tsconfig-paths/register
},
"watch": [
"src/**/*.ts"
]
}
nodemon ./src/index.ts
module-alias
const { join } = require('path')
const moduleAlias = require('module-alias')
moduleAlias.addAlias('~', join(__dirname, 'lib'))
moduleAlias()
require('./lib')
Nullable type + Type Guards
import * as React from 'react'
export interface User {
_id: string
name: string
avatarUrl?: string
}
const findUserById = (id: string): User | null => {
// ...
return null
}
const props = {
router: {
params: {
id: ''
}
}
}
const user = findUserById(props.router.params.id)
const name = (<div>{user.name}</div>) // error
Union type + Type Guards
import * as React from 'react'
export interface User {
_id: string
name: string
avatarUrl?: string
}
type UserInfo = User | string
const findUserById = (id: string): User | null => {
// ...
return null
}
declare const userInfo: UserInfo
let user: User | null = null
if (typeof userInfo === 'string') {
user = findUserById('')
} else {
user = userInfo
}
const name = (<div>{user?.name}</div>)
Literal type
interface User {}
type UserType = 'admin' | 'customer' | 'guest'
export function findUserByType(userType: UserType): User[] {
return []
}
findUserByType('admin')
Literal type
interface User {}
type UserType = 'admin' | 'customer' | 'guest'
export function findUserByType(userType: UserType): User[] {
return []
}
findUserByType('admin')
keyof 运算符
interface User {
_id: string
name: string
avatarUrl: string
age: number
birthDay: Date
}
function checkPropertyValidate(user: User, key: keyof User) {
}
declare const user: User
checkPropertyValidate(user, '_id')
mapped type
interface User {
_id: string
name: string
avatarUrl: string
age: number
birthDay: Date
}
type AsyncUser = {
[K in keyof User]: Promise<User[K]>
}
function fillUserAsync(user: User): AsyncUser {
return Object.create({})
}
declare const user: User
fillUserAsync(user).age
函数重载
interface UseUserIDConfig {
useId: true
// ....
}
interface UseUserEntityConfig {
useId: false
// ...
}
function getUser(config: UseUserIDConfig, id: string): void;
function getUser(config: UseUserEntityConfig): void;
function getUser(config: UseUserIDConfig | UseUserEntityConfig, id?: string) {
}
getUser({ useId: true })
https://www.typescriptlang.org/docs/handbook/advanced-types.html
https://github.com/Brooooooklyn/typescript-example-projects/tree/master/javascript-migrate-to-typescript