faker在mock数据的时候常常会用到,但是这次用到的时候遇到一个问题。
问题:
import * as faker from 'faker';
faker.locale = 'zh_CN';
原因:
es6在import一个模块的时候会自动设置变量为readonly/常量. 比如
// hello-world/index.d.ts
declare namespace Hello {
interface World {
name: string;
}
}
declare const hello: Hello.World;
declare module 'hello-world' {
export = hello;
}
// 项目目录
import * as hello from 'hello-world';
hello.name // readonly
解决:
import hello = require('hello-world');
hello.name = 'yuejiang';
const faker = require('faker'); // 也可以,但是会报建议使用import,编辑器无法推导类型
import faker = require('faker');
faker.locale = 'zh_CN';