Hang on a second, I’m not familiar with Typescript in the slightest but what is that bitwise or (I assume?) doing in the ruc_id assignment? If you want ruc_id to be a number type could you not do let ruc_id: number = null;?
Yes, the code is from developer who probably didn't even know what is he doing. It's called programming by coincidence. The worst thing is that the whole codebase is programmed like this.
How do you mean? 👀 I mean, when used correctly by the looks of it, it can bring a huge amount of clarity and structure. Consequently though if not used well it could make things an absolute mess
Well I do enjoy engineering but you don’t have to always over engineer to use ts. The intellisense you get from your IDE worths all the effort took into creating types.
! Also happens to be the correct way to deal with this, I have to use it sometimes when using typescript for web frontend stuff since it's the only way to make the compiler know a certain element is going to be in the DOM since tsc doesn't read the HTML files in your project as far as I know.
A new ! post-fix expression operator may be used to assert that its operand is non-null and non-undefined in contexts where the type checker is unable to conclude that fact. Specifically, the operation x! produces a value of the type of x with null and undefined excluded. Similar to type assertions of the forms <T>x and x as T, the ! non-null assertion operator is simply removed in the emitted JavaScript code.
// Compiled with --strictNullChecks
function validateEntity(e?: Entity) {
// Throw exception if e is null or invalid entity
}
function processEntity(e?: Entity) {
validateEntity(e);
let s = e!.name; // Assert that e is non-null and access name
8
u/_seumoose Jan 21 '22 edited Jan 21 '22
Hang on a second, I’m not familiar with Typescript in the slightest but what is that bitwise or (I assume?) doing in the ruc_id assignment? If you want ruc_id to be a number type could you not do
let ruc_id: number = null;?17
u/diesmilingxx Jan 21 '22
What does !. do?
16
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Jan 21 '22
Image Transcription: Code
I'm a human volunteer content transcriber and you could be too! If you'd like more information on what we do and why we do it, click here!
5
u/thebritisharecome Jan 21 '22
I was worried the developer didn't know how to destructure objects for a moment, but I see they saved it for a single line
1
u/[deleted] Jan 21 '22
what colorscheme is that?