admin管理员组文章数量:1426929
I don't understand the reasoning behind non nullable types and Typescript's strictNullChecks
flag. I always set the value of a variable to null when I am sure I am done with it to release it from memory and the GC can collect it. My tests have shown that it speeds up the execution of the JavaScript code by many times, especially in long, and deep loops. Is this a bad practice? Should some types of variables be set to null and not others?
I don't understand the reasoning behind non nullable types and Typescript's strictNullChecks
flag. I always set the value of a variable to null when I am sure I am done with it to release it from memory and the GC can collect it. My tests have shown that it speeds up the execution of the JavaScript code by many times, especially in long, and deep loops. Is this a bad practice? Should some types of variables be set to null and not others?
- 1 just out of curiosity, could you share the tests you're talking about? – georg Commented Sep 28, 2019 at 10:17
- The tests I referred to were basically running some code on an older model smart phone and crashing it with an out of memory code, then tweaking the code until it would run again. Consequently the code also ran ~4X faster in a desktop browser (Firefox) as well, probably by avoiding the GC. The code was parsing several MB of CSV text to JSON and storing it in a database. Quite plicated to show here – scripter Commented Sep 28, 2019 at 10:28
-
2
My advice if you're only using
null
to free up memory (hey, you're the boss): turn on--strictNullChecks
, declare your variables as non-nullable (e.g.,let x: string = "hello";
and then if you must set a variable tonull
when you're done with it, lie to the piler about it (e.g.,x = null!;
where the!
operator is a non-null assertion); safe enough if you're really not using it anymore. Happy to turn this into an answer if it meets your needs. Good luck! – jcalz Commented Sep 29, 2019 at 0:21 -
Thanks @jcalz
x = null!;
fixes all my errors with a simple find and replace while still allowing thestrictNullChecks
flag to be used. It probably doesn't answer the entire question but was a super tip! – scripter Commented Sep 30, 2019 at 7:15
2 Answers
Reset to default 3The reasoning is to prevent some run-time errors including console.log(null.printWhyNullIsBad());
by catching those errors at the pilation time while assuming that people who write loops so deep and so nested that it stretches the modern JS garbage collectors to their limits can write that kind of code in C/C++ and run it using WebAssembly.
In most cases you don't have to delete variable values yourself. It may be a design flaw. Also note that if you want to delete an object property value, it may be more consistent to use that:
delete myObject.attr; // set 'undefined'
I don't understand the reasoning behind non nullable types and Typescript's strictNullChecks flag.
To understand the logic behind, check this sample:
// strictNullChecks: false
interface Sample {
color: string;
width: number;
}
function doSomething(sample: Sample): Sample {
if(sample.width > 10) {
return null;
}
return sample;
}
const first: Sample = {
color: 'red',
width: 15
};
const second: Sample = doSomething(first);
console.log(second.width); // Uncaught TypeError: Cannot read property 'width' of null
Playground
This code piles only without strictNullChecks
flag. In this context when the code runs we have a beautiful error: Uncaught TypeError: Cannot read property 'width' of null
.
Check the function doSomething
. It returns a Sample
type, but without the flag we can return null
. The problem here is that when we call this function, the value returned will be manipulated as a Sample
=> There is a inconsistence between the type and its real value type. It's a huge cause of errors.
I like to think that TS typings must reflect at the best real types of values.
本文标签: javascriptWhy does Typescript discourage setting values to nullStack Overflow
版权声明:本文标题:javascript - Why does Typescript discourage setting values to null? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745475318a2659937.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论