[英]Use 'strictNullChecks' annotation on one function/method
我有這個方法: 函數體中有多個return語句,我想避免null/undefined return語句。 有沒有我可以添加到這個方法的注釋,比如: ...
[英]Use 'strictNullChecks' annotation on one function/method
我有這個方法: 函數體中有多個return語句,我想避免null/undefined return語句。 有沒有我可以添加到這個方法的注釋,比如: ...
[英]Compare undefined with numbers with strictNullChecks enabled
strictNullChecks是一個很好的選擇,它可以幫助開發人員避免許多潛在的運行時錯誤。 所以我最近開始在啟用此選項的情況下重構我的代碼。 但后來我遇到了一個小但有點煩人的場景: 我有使用這種語法來獲取非空數組的習慣,利用 JavaScript 功能,使用<或>比較任何undef ...
[英]Type narrowing: checking variable object key existence when noUncheckedIndexedAccess is true
我的tsconfig.json中有"noUncheckedIndexedAccess": true 。 開關的全部要點是在訪問索引之前強制檢查該項目是否存在。 我很難用可變鍵檢查 object: 說我有以下結構:const items: Record<string, {name: string ...
[英]Is there a good way of telling typescript all array elements exist in a Map?
假設我正在從以 id 作為鍵的對象數組創建 Map,然后從具有相同 id 的不同數組訪問 map: 如果我有“strictNullChecks”:true,那么最后一行會拋出一個“對象可能是'未定義'”錯誤,即使它永遠不會是未定義的。 有沒有辦法告訴 typescript 數組中的元素存在於 ma ...
[英]Can I disable distinguishing between null and undefined in TypeScript strict null-checking mode?
我目前正在將大型 TypeScript 代碼庫轉換為嚴格的空檢查。 代碼庫有許多帶有可選成員的類型: 此外,它使用類型Nullable<T> = T | null Nullable<T> = T | null ,並且有很多return null語句。 現在我面臨許多編 ...
[英]Type alias from property that might be null with strictNullChecks
我正在使用graphql-codegen/typescript-apollo-angular生成 typescript 代碼,可以在我們的 Angular 應用程序中使用。 我們決定啟用strictNullChecks ,唯一的問題是我們使用以下模式來指定 API 返回的內部節點的類型: 在我的情況 ...
[英]Can I solve this one without strictNullChecks: false?
我想知道如何在不將strictNullchecks設置為 false 的情況下解決這個問題。 或者 如您所知,當我將strictNullChecks設置為 true 時,TypeScript 會顯示類似“對象可能未定義”的錯誤。 將strictNullChecks設置為false 是最佳選 ...
[英]Getting Typescript strictNullChecks to work with undefined returning vanilla js functions
使用慣用的 js 在錯誤時返回未定義,轉換為 TS 當在新的 TS 代碼中使用乘法時,我得到編譯器認為 doStuff 可以返回 undefined 的問題,但它不能。 因此,我嘗試編寫由安全 TS 代碼調用的此函數的“不安全”版本,將安全版本留給常規 js 代碼。 由於 unsafeMu ...
[英]Allow nullable operands in less- or greater-than comparisons in TypeScript
在 JavaScript 中, 關系表達式中的空操作數被視為 0 : 但是在 TypeScript 中,如果我為f提供類型注釋並打開嚴格的空檢查,那么我會得到編譯器錯誤Object is possibly 'null' : 有沒有辦法告訴編譯器這里可以接受可為空的操作數? 我可以用f()上 ...
[英]Typescript strictNullChecks does not narrow type
我有一個簡單的情況,即使我明確檢查未定義, strictNullChecks也不會縮小類型。 錯誤信息是: 我如何幫助 TS 編譯器解決這個問題? TS 版本 3.7.2 ...
[英]TypeScript: Can I check if a type contains undefined even when strictNullChecks is false?
我們在 redux-starter-kit 中遇到了一個問題,其中庫的用戶禁用了 strictNullChecks,並且我們的類型測試之一是短路,返回不同情況的類型。 此測試應返回 True-Parameter 或 False-Parameter,具體取決於 P 參數是否包含 undefined。 ...
[英]Typescript strictNullChecks error when not possible
我正在嘗試為我的項目打開strictNullChecks設置,但是以下代碼片段中有一個非常奇怪的錯誤: 錯誤消息的if語句中帶有下划線的錯誤消息帶有toaster.brandName ,並帶有以下錯誤文本: error TS2532: Object is possibly 'undefi ...
[英]Why does TypeScript infer the 'never' type when reducing an Array with concat?
代碼比語言說得更好,所以: 該代碼非常愚蠢,並返回一個復制的數組...... TS 抱怨 concat 的參數:TS2345:“字符串”類型的參數不可分配給“ConcatArray”類型的參數。 ...
[英]TypeScript: check that the required properties of an argument are defined before passing it to a function call
這不會編譯( 操場 ): 是否有比params as any更優雅的解決方法? ...
[英]createStore with strict typescript
當使用上面的代碼並strict啟用typescript時,我收到以下警告: 我理解TS嚴格模式是什么,但我不明白為什么它將我的狀態解釋為可能undefined 我能做錯什么? 或者,我怎么可能對付沒有做一些懶惰像誤差as any ? ...
[英]Typescript Why type guard does not work as expected for array of objects of type null-able union(strictNullChecks enabled)
無論類型類型為守衛,Transpiler都會引發錯誤: 這里沒有錯誤: ...
[英]Ngrx: Property '[Symbol.observable]' is missing in type 'Store<boolean>' error
我在Angular v5應用程序中使用了NGRX v4.1.1(帶有"strictNullChecks": true ,盡管似乎無關緊要)。 我發現商店有錯誤。 給定以下內容: 打字稿在this.store.select()上this.store.select()錯誤: ...
[英]TypeScript with strict null checking - what about array access?
在 TypeScript 中,如果啟用了嚴格的 null 檢查,我希望編譯器阻止我將null或undefined值分配給變量,除非它承認null 。 但是,數組訪問似乎允許繞過此檢查。 例子: TypeScript Playground 上的可運行版本(注意:必須在“選項”對話框中啟用 ...
[英]Typescript and filter Boolean
考慮以下啟用了strictNullChecks的代碼: 由於以下原因,它無法編譯: 但絕對清楚的是, false將被.filter(Boolean)過濾掉。 與null相同的問題。 有沒有辦法(除了寫成as number[] )來標記該值不包含false或null ? ...
[英]How to tell Typescript that *in this instance* a function's return type is never null
我正在嘗試讓Angular 5項目中的打字稿strictNullChecks工作。 我有一個表格: 我能得到rememberMe使用控制this.signinForm.get('rememberMe') 但是, FormGroup#get方法的返回是AbstractControl ...