जवाबों:
जावास्क्रिप्ट में सभी क्षेत्रों (और टाइपस्क्रिप्ट में) का मान हो सकता है nullया undefined।
आप फ़ील्ड को वैकल्पिक बना सकते हैं जो अशक्त से अलग है।
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';
}
"strict" : false
salary:number|null;यदि आप करते हैं तो salary?:number; salary = null;आपको एक त्रुटि मिलेगी। हालांकि, salary = undefined;इस मामले में ठीक काम करेगा। समाधान: संघ का उपयोग करें '|'
संघ का मामला इस मामले में मेरे दिमाग में सबसे अच्छा विकल्प है:
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 };
संपादित करें: इस काम के लिए के रूप में की उम्मीद के लिए, आप सक्षम होना चाहिए strictNullChecksमें tsconfig।
अधिक सी # होने के लिए, इस Nullableप्रकार को परिभाषित करें:
type Nullable<T> = T | null;
interface Employee{
id: number;
name: string;
salary: Nullable<number>;
}
बक्शीश:
Nullableटाइपस्क्रिप्ट प्रकार में निर्मित व्यवहार करने के लिए , इसे global.d.tsरूट स्रोत फ़ोल्डर में परिभाषा फ़ाइल में परिभाषित करें । इस पथ ने मेरे लिए काम किया:/src/global.d.ts
emp: Partial<Employee>, तो हम कर सकते हैं emp.idया emp.nameआदि लेकिन अगर हमारे पास है emp: Nullable<Employee>, तो हम नहीं कर सकते हैंemp.id
?वैकल्पिक फ़ील्ड में एक प्रश्न चिह्न जोड़ें ।
interface Employee{
id: number;
name: string;
salary?: number;
}
आप निम्नलिखित की तरह केवल एक उपयोगकर्ता-परिभाषित प्रकार लागू कर सकते हैं:
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];
मैं थोड़ी देर पहले यह एक ही सवाल था .. ts में सभी प्रकार अशक्त हैं, क्योंकि शून्य सभी प्रकारों का एक उपप्रकार है (इसके विपरीत, उदाहरण के लिए, scala)।
देखें कि क्या यह फ़्लोचार्ट मदद करता है - https://github.com/bcherny/language-types-comparison##ypescript
void'सभी प्रकारों' ( नीचे प्रकार ) का उपप्रकार होने के लिए , इस सूत्र को देखें । इसके अलावा आपने जो चार्ट स्कैला के लिए दिया है वह भी गलत है। Nothingscala में, वास्तव में, निचला प्रकार है। टाइपस्क्रिप्ट, एटीएम, में निचला प्रकार नहीं होता है , जबकि स्कैला करता है ।
अशक्त प्रकार रनटाइम त्रुटि को लागू कर सकता है। इसलिए मुझे लगता है कि संकलक विकल्प का उपयोग --strictNullChecksकरना number | 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
typescript@nextअब।)