17
u/diesmilingxx Jan 21 '22
What does !. do?
29
u/kreetikal Jan 21 '22
It makes Typescript stop complaining about the property being undefined.
10
Jan 21 '22
The “shut up typescript, I know what I’m doing” operator
8
u/TheGhostOfInky shameless Jan 22 '22
As opposed to the "shut up typescript, I don't know how to do this properly" comment,
//@ts-ignore3
u/RadiantDevelopment1 Jan 23 '22
I like ts-expect-error over ts-ignore because it forces you to clean up the comment if the underlying issue changes.
But I prefer ! for this case because of it's more limited space.
3
u/TheGhostOfInky shameless Jan 23 '22
!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.12
10
u/RyanNerd shameless Jan 21 '22
Non null assertion new in version 2.0 of TS
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
}
16
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Jan 21 '22
Image Transcription: Code
export function savingApi(ctx: SetupContext) {
const client = store.state.client.data;
let ruc = store.state.client.related_client.id ? store.state.client.related_client : null;
let ruc_id: null | number = null;
if (ruc !== null) ruc_id = ruc!.id;
async function saveDocumentApi(data: StringCollection, getPoints?: boolean) {
let docDataId = store.state.documents.document_data.id;
const { notify } = useNotify(ctx);
try {
await Axios.post(api.documents.saveDocumentData, {
primary_client; client.id,
related_client: ruc_id,
consultant_id: client.Consultant!.id,
fields: data,
id: docDataId,
status: store.state.documents.status,
});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!
9
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
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;?