tslint.json

1. Genere tslint.json

Primero descargue tslint, preste atención a que debe ser global, de lo contrario no tendrá efecto

cnpm i tslint --save -g

Genere tslint.json

tslint --init

En este momento del proyecto:

2. Utilice interfaces para evitar la detección de atributos redundantes

Si no hay una definición en la interfaz, pero se agrega un nuevo campo durante el uso, aparecerá un mensaje de error.Se presentan dos soluciones.

(1) Modificar la interfaz

interface paramsCheck {
  activityId: string,
  age: number,
  [prop: string]:any // 其他未定义类型的数据,属性名是string类型
}

const getUserInfo = (params: paramsCheck):string => {
  return `activityId: ${params.activityId}, age: ${params.age}`
}

var res = getUserInfo({
  activityId: '23',
  age: 23,
  sex: '女'
})

console.log(res)

(2) No pases directamente, usa el objeto

interface paramsCheck {
  activityId: string,
  age: number
}

const getUserInfo = (params: paramsCheck):string => {
  return `activityId: ${params.activityId}, age: ${params.age}`
}

let info = {
  activityId: '23',
  age: 23,
  sex: '女'
}
var res = getUserInfo(info)

console.log(res)

Esto no reportará un error

Solo lectura definido en la interfaz: solo lectura

interface paramsCheck {
  activityId: string,
  readonly age: number
}
const getUserInfo = (params: paramsCheck):string => {
  params.age = 22 // 修改
  return `activityId: ${params.activityId}, age: ${params.age}`
}

Error de aviso: 

Herencia de la interfaz: use la palabra clave extiende

interface Vegetables {
  color: string
}

interface Tomatos extends Vegetables {
  type: string
}

const tomato:Tomatos = {
  color: 'red',
  type: 'am'
}

Superponer los dos nombres

 

Supongo que te gusta

Origin blog.csdn.net/Luckyzhoufangbing/article/details/108639248
Recomendado
Clasificación