web-dev-qa-db-ja.com

TypeScriptで型をnull許容として宣言する方法

TypeScriptのインターフェースがあります。

interface Employee{
   id: number;
   name: string;
   salary: number;
}

私は(給与)をnull入力可能フィールドにしたいのですが(C#でできるように)。これはTypeScriptで可能ですか?

164
Amitabh

JavaScript(およびTypeScript)のすべてのフィールドは、値nullまたはundefinedを持つことができます。

フィールドをオプションにすることができます。これはNULL可能とは異なります。

interface Employee1 {
    name: string;
    salary: number;
}

var a: Employee1 = { name: 'Bob', salary: 40000 }; // OK
var b: Employee1 = { name: 'Bob' }; // Not OK, you must have 'salary'
var c: Employee1 = { name: 'Bob', salary: undefined }; // OK
var d: Employee1 = { name: null, salary: undefined }; // OK

// OK
class SomeEmployeeA implements Employee1 {
    public name = 'Bob';
    public salary = 40000;
}

// Not OK: Must have 'salary'
class SomeEmployeeB implements Employee1 {
    public name: string;
}

と比べて:

interface Employee2 {
    name: string;
    salary?: number;
}

var a: Employee2 = { name: 'Bob', salary: 40000 }; // OK
var b: Employee2 = { name: 'Bob' }; // OK
var c: Employee2 = { name: 'Bob', salary: undefined }; // OK
var d: Employee2 = { name: null, salary: 'bob' }; // Not OK, salary must be a number

// OK, but doesn't make too much sense
class SomeEmployeeA implements Employee2 {
    public name = 'Bob';
}
211
Ryan Cavanaugh

この場合、私の考えではユニオンタイプが最良の選択です。

interface Employee{
   id: number;
   name: string;
   salary: number | null;
}

// Both cases are valid
let employe1: Employee = { id: 1, name: 'John', salary: 100 };
let employe2: Employee = { id: 1, name: 'John', salary: null };

編集:これが期待どおりに動作するためには、strictNullCheckstsconfigを有効にする必要があります。

83
bjaksic

オプションのフィールドに疑問符?を追加するだけです。

interface Employee{
   id: number;
   name: string;
   salary?: number;
}
27
Miguel Ventura

よりC#に似たものにするには、Nullable型を次のように定義します。

type Nullable<T> = T | null;

interface Employee{
   id: number;
   name: string;
   salary: Nullable<number>;
}
21
Tim Santeford
type MyProps = {
  workoutType: string | null;
};
4
Ritwik

voidはすべての型のサブタイプなので(scalaとは異なり)、tsのすべての型はNULL可能です。

このフローチャートが役立つかどうかを確認してください - https://github.com/bcherny/language-types-comparison#TypeScript

4
bcherny

Nullable型はランタイムエラーを引き起こす可能性があります。したがって、コンパイラオプション--strictNullChecksを使用し、number | nullを型として宣言することをお勧めします。ネストされた関数の場合も、入力タイプはnullですが、コンパイラーがそれが何を壊す可能性があるかわからないため、!(感嘆符)を使用することをお勧めします。

function broken(name: string | null): string {
  function postfix(epithet: string) {
    return name.charAt(0) + '.  the ' + epithet; // error, 'name' is possibly null
  }
  name = name || "Bob";
  return postfix("great");
}

function fixed(name: string | null): string {
  function postfix(epithet: string) {
    return name!.charAt(0) + '.  the ' + epithet; // ok
  }
  name = name || "Bob";
  return postfix("great");
}

参照。 https://www.typescriptlang.org/docs/handbook/advanced-types.html#type-guards-and-type-assertions

2
Margaux

次のようなユーザー定義型を実装できます。

type Nullable<T> = T | undefined | null;

var foo: Nullable<number> = 10; // ok
var bar: Nullable<number> = true; // type 'true' is not assignable to type 'Nullable<number>'
var baz: Nullable<number> = null; // ok

var arr1: Nullable<Array<number>> = [1,2]; // ok
var obj: Nullable<Object> = {}; // ok

 // Type 'number[]' is not assignable to type 'string[]'. 
 // Type 'number' is not assignable to type 'string'
var arr2: Nullable<Array<string>> = [1,2];
2

未定義としてあなたの数の値を入れて

var user: Employee = { name: null, salary: undefined }; 
0