TS原生的Readonly只会限制一层写入操作,我们可以利用递归来实现深层次的Readonly。但要注意,TS对最大递归层数做了限制,最多递归5层。
type DeepReadony<T> = {
readonly [P in keyof T]: DeepReadony<T[P]>
}
interface SomeObject {
a: {
b: {
c: number;
| import axios, { AxiosRequestConfig, AxiosResponse } from 'axios'; | |
| import { Observable } from 'rxjs'; | |
| const fromRequest = (request: AxiosRequestConfig) => | |
| new Observable<AxiosResponse>( | |
| (o) => { | |
| const source = axios.CancelToken.source(); | |
| o.add(() => source.cancel('Operation canceled by the user.')); |
| "use strict"; | |
| /** | |
| * Hypertext Transfer Protocol (HTTP) response status codes. | |
| * @see {@link https://en.wikipedia.org/wiki/List_of_HTTP_status_codes} | |
| */ | |
| enum HttpStatusCode { | |
| /** | |
| * The server has received the request headers and the client should proceed to send the request body |
TS原生的Readonly只会限制一层写入操作,我们可以利用递归来实现深层次的Readonly。但要注意,TS对最大递归层数做了限制,最多递归5层。
type DeepReadony<T> = {
readonly [P in keyof T]: DeepReadony<T[P]>
}
interface SomeObject {
a: {
b: {
c: number;
我在项目中遇到这样一种场景,需要获取一个类型中所有value为指定类型的key。 例如,已知某个React组件的props类型
interface SomeProps {
a: string
b: number
c: (e: MouseEvent) => void
d: (e: TouchEvent) => void
}
| /* eslint-disable prefer-template */ | |
| const path = require('path'); | |
| const aliases = require('./aliases'); | |
| // /////////////////////////////////////////////////////////////// | |
| // ////////////////// PLUGINS //////////////////////////////// | |
| // /////////////////////////////////////////////////////////////// | |
| const commonPlugins = [ |
| /** | |
| @description 页面垂直平滑滚动到指定滚动高度 | |
| @author zhangxinxu(.com) | |
| */ | |
| var scrollSmoothTo = function (position) { | |
| if (!window.requestAnimationFrame) { | |
| window.requestAnimationFrame = function(callback, element) { | |
| return setTimeout(callback, 17); | |
| }; | |
| } |
Say we have a prop.users of the shape:
const users = [
{username: 'bob', age: 30, tags: [{name: 'work', id: 1}, {name: 'boring', id: 2}]},
{username: 'jim', age: 25, tags: [{name: 'home', id: 3}, {name: 'fun', id: 4}]},
{username: 'jane', age: 30, tags: [{name: 'vacation', id: 5}, {name: 'fun', id: 4}]}
];
Either you use .babelrc to specify environment specific settings
(plugins or transforms for example) using the env key:
{
"presets": ["es2015", "stage-0", "react"],
"env": {
"development": {
"plugins": [
["transform-object-rest-spread"],
["transform-react-display-name"],| 今天看到一个数组的声明是这样的: | |
| var dotenvFiles = [ | |
| `${paths.dotenv}.${NODE_ENV}.local`, | |
| `${paths.dotenv}.${NODE_ENV}`, | |
| NODE_ENV !== 'test' && `${paths.dotenv}.local`, | |
| paths.dotenv, | |
| ].filter(Boolean); | |
| //result: | |
| //['XX/XX.local', 'XX/XX'] |
| //大写单词的首字母 | |
| const capitalize = (str, lowerRest = false) => str.slice(0, 1).toUpperCase() + (lowerRest ? str.slice(1).toLowerCase() : str.slice(1)); | |
| // capitalize(‘myName’, true) -> ‘Myname’ | |
| //note: 使用slice(0,1)和toUpperCase()大写第一个字母,slice(1)获取字符串的其余部分. | |
| //大写每个单词的首字母 | |
| const capitalizeEveryWord = str => str.replace(/\b[a-z]/g, char => char.toUpperCase()); |