Skip to content

Instantly share code, notes, and snippets.

View udede's full-sized avatar
👋
Turning coffee into algorithms since 2003

Francesco Lisandro udede

👋
Turning coffee into algorithms since 2003
View GitHub Profile
@udede
udede / reactive-forms-template.ts
Last active February 24, 2021 01:34
Reactive form Template
<section class="sample-app-content">
<h1>Esempio Reactive Form</h1>
<form [formGroup]="formElement" (ngSubmit)="accedi()">
<p>
<label>Email:</label>
<input type="text" formControlName="email">
</p>
<p>
<label>Password:</label>
<input type="password" formControlName="password">
import {NgModule} from "@angular/core";
import {ReactiveFormsModule} from "@angular/forms";
@Component({...})
export class App { }
@NgModule({
declarations: [App],
imports: [BrowserModule, ReactiveFormsModule],
bootstrap: [App]
@udede
udede / assertion-as.ts
Created February 23, 2021 03:23
Assertion as
let altezza: any = 1.82;
let altezzaMimmo = altezza as number;
console.log(typeof(altezzaMimmo)); // number
@udede
udede / assertion-par.ts
Created February 23, 2021 03:18
Assertion Parentesis
// Esempio su un numero
let altezza: any = 1.82;
let altezzaMimmo = <number> altezza;
console.log(typeof(altezzaMimmo)); // number
// Esempio su un oggetto
// Non corretto
let mimmo = {};
@udede
udede / never-type.ts
Last active February 23, 2021 03:03
Never Type
function generaErrore(messaggio: string): never {
throw new Error(messaggio);
}
function elabora(): never {
while (true) {
console.log('Elaboro ed elaboro senza mai fermarmi.');
}
}
@udede
udede / any-type.ts
Created February 23, 2021 02:37
Any Type
let valore: any;
valore = true; // OK
valore = 42; // OK
valore = "Wow Mimmo"; // OK
valore = []; // OK
valore = {}; // OK
valore = Math.random; // OK
valore = null; // OK
valore = undefined; // OK
type IntersezioneTipi1 = unknown & null; // null
type IntersezioneTipi2 = unknown & undefined; // undefined
type IntersezioneTipi3 = unknown & string; // string
type IntersezioneTipi4 = unknown & number[]; // number[]
type IntersezioneTipi5 = unknown & any; // any
type UnioneTipi1 = unknown | null; // unknown
type UnioneTipi2 = unknown | undefined; // unknown
type UnioneTipi3 = unknown | string; // unknown
type UnioneTipi4 = unknown | number[]; // unknown
type UnioneTipi5 = unknown | any; // any
const sconosciuta: unknown = "Ciao Mimmo";
const testo: string = sconosciuta as string;
const altroTesto = testo.toUpperCase();
console.log(altroTesto); // "CIAO MIMMO"
// Considera che TypeScript non esegue controlli speciali per assicurarsi che
// l'asserzione del tipo sia effettivamente valida. Il controllo del tipo presume che
// tu sappia cosa stai facendo e pensa che il tipo utilizzato sia quello corretto.
let isStringArray = (sconosciuta: unknown): sconosciuta is string[] => {
return (
Array.isArray(sconosciuta) &&
sconosciuta.every(el => typeof el === "string")
);
}
console.log(isStringArray([2,3,4,'Mimmo','Giovanni'])); // false
console.log(isStringArray(['Mimmo','Sandra','Leonardo','Filippo','Susanna'])); // true