Modo estrito no TypeScript: descrição da bandeira, exemplos

--strict estrito inclui os seguintes sinalizadores:


--strictNullChecks
--sempreStrict
--noImplicitAny
--noImplicitThis
--strictBindCallApply
--strictFunctionTypes
--strictPropertyInitialization

Damos exemplos e tentamos entender em um só lugar o que tudo isso significa.

// I. --strictNullChecks


O famoso problema com o NPE (exceção de ponteiro nulo, erro de bilhão de dólares) no contexto do TS.
Por padrão, no TS todos os tipos são Anuláveis ​​e isso significa que podemos passar "indefinidos" | "Nulo" onde qualquer outro tipo é esperado (mesmo um primitivo):

const bar1: { foo: number } = undefined;
const bar2: { foo: number } = null;
const bar3: number = null;
const bar4: string = null;

Exemplos mais interessantes são uma chamada de método, que pode não ser

declare var smth: { optionalMethod?(): string; };
smth.optionalMethod();

Também está implícito que não podemos retornar "indefinidos" | "Nulo" onde isso claramente não é esperado

function getIt(): { data: number } {
  // Type 'undefined' is not assignable to type '{ data: number; }'
  return undefined;
}
getIt().data;
 

Teremos que indicar explicitamente que "indefinido" pode retornar e somente depois disso ocorreremos um erro

function getIt(): { data: number } | undefined {
  return undefined;
}
// “Object is possibly 'undefined'”
getIt().data;
 

E como um bônus - operações mais seguras, onde pode não haver resultado, haverá um erro com a bandeira ativada e você terá que verificar explicitamente se “encontrar” encontrou algo:

// Object is possibly 'undefined'
[{ name: 'John', age: 4 }]
 .find(el => el.age === 42)
 .name;
 

// II. --alwaysStrict


Adiciona anotação 'use strict' a cada arquivo, tornando o comportamento do JS mais explícito

// III. --noImplicitAny


Proíbe o uso implícito de 'any' no TS, ou seja, código sem anotação de tipo

 // Parameter 'a' implicitly has an 'any' type
 function id(arg) {
   return arg;
 }

Grande ajuda com importações não tipadas de bibliotecas de terceiros, sugerindo a instalação de definições de tipo

 /* Could not find a declaration file for module '3rd-party-lib'. '/node_modules/3rd-party-lib/index.js' implicitly has an 'any' type.

Try `npm install @types/3rd-party-lib` if it exists or add a new declaration (.d.ts) file containing `declare module '3rd-party-lib';`*/

import * as session from '3rd-party-lib';
 

// IV. --strictBindCallApply


Inclui uma verificação de tipo "mais rigoroso" para "bind" / "call" / "apply", sem sinalizador - tudo isso é um TS válido.

 function getFullName(name: string, surname: string): string {
   return name + surname;
 }
 
 getFullName.call(null, 'John', 42);
 getFullName.apply(null, ['John', 42]);
 getFullName.bind(null)('John');
 getFullName.bind(null, 'John')();
 getFullName.bind(null, 'John')(42);
 

// V. --strictPropertyInitialization + --strictNullChecks


Ajuda a rastrear que todas as propriedades foram inicializadas no construtor; você também deve habilitar --strictNullChecks para desativar os tipos Nullable.

 class User {
// Property 'name' has no initializer and is not definitely assigned in the constructor
   name: string;
 }

No entanto, se a atribuição não estiver no próprio construtor, convença o TS de que está tudo bem.

 class User2 {
  // Property 'name' has no initializer and is not definitely assigned in the constructor
   name: string;
     
   constructor(name: string) {
     this.initializeName();
   }
 
   initializeName() {
     this.name = 'John'
   }
 }

Se você não conseguiu convencer o TS de que a propriedade será exatamente inicializada, você pode dizer "Juro pela mãe, definitivamente inicializarei!" ou mais brevemente "!"

class User3 {
   // definite assignment assertion
   name!: string;
 }
 


// VI. --strictFunctionTypes


Remove um cheque bivariada, para argumentos.

Variant na programação, em suma - esta é a capacidade de passar supertipo / subtipo lá, onde Tipo é esperado. Por exemplo, existe uma hierarquia Shape -> Circle -> Rectangle. É possível transferir ou retornar uma Shape / Rectangle se Circle for esperado ? Opção de

programação habr , SO

interface Shape { name: string };
interface Circle extends Shape { width: number };
interface Rectangle extends Circle { height: number };
 
declare var logSC: (figure: Shape) => Circle;
declare var logRC: (figure: Rectangle) => Circle;
 
declare var logCC: (figure: Circle) => Circle;
 
declare var logCS: (figure: Circle) => Shape;
declare var logCR: (figure: Circle) => Rectangle;
 
declare var wlogBB: (fn: (figure: Circle) => Circle) => void;
 
wlogBB(logCC);
wlogBB(logSC);
wlogBB(logCR);
 
// always Error
wlogBB(logCS);
// Error with --strictFunctionTypes
wlogBB(logRC);

Entende-se que a função não deve alterar o argumento passado (atuando como produtor de tipo), não há erros no TS, de fato - existem

const squares: Square[] = [{ name: 'Square', width: 5 }];
 
// function looks like a consumer of argument
function addSmth(arg: Shape[]) {
 // work with argument as a producer
 arg.push({ name: 'Square' });
}
addSmth(squares);
 

// VII. --noImplicitThis


Se a função for definida fora do objeto / classe, o TS solicitará que você indique explicitamente o que "this" se refere ao uso do primeiro pseudo-argumento chamado "this"

// TS force to add annotation for 'this'
 function getName(this: { name: string }, surname: string): string {
   return this.name;
 }
 
 // The 'this' is not assignable
 getName.call({}, 'Smith');
 getName.apply({}, ['Smith']);
 getName.bind({})('Smith');

As chamadas serão válidas

const somePerson = { name: 'John', getName };
const fullName: string = somePerson.getName('Smith')
 
getName.call({name: 'John'}, 'Smith');
getName.apply({name: 'John'}, ['Smith']);
getName.bind({name: 'John'})('Smith');
 

Funções de construtor podem causar problemas

function Person(this: { name: string }, name: string) {
   this.name = name;
 }
 // 'new' expression, whose target lacks a construct signature
 // Use class )
 const person = new Person('John');
 

Um bônus interessante é adicionar uma comparação de métodos de ligação de contexto para classes.

class A {
   x = 42;
 
   constructor() {
     this.getBound = this.getBound.bind(this);
   }
 
   getSimple(): number {
     return this.x;
   }
 
   // Has to add type for 'this', TS dont force it
   getSimpleAnnotated(this: A): number {
     return this.x;
   }
 
   getArrow = (): number => this.x;
 
   getBound(this: A): number {
     return this.x;
   }
 }
 
 const a = new A();
 
 // False positive: TS - ok, Runtime - error
 const getSimple = a.getSimple;
 getSimple();
 
 // Correct: TS - error, Runtime - error
 const getSimpleAnnotated = a.getSimpleAnnotated;
 getSimpleAnnotated();
 
 // Correct: TS - ok, Runtime - ok
 const getArrow = a.getArrow;
 getArrow();
 
 // False negative: TS - error, Runtime - ok
 const getBound = a.getBound;
 getBound();
 

All Articles