Last active
June 14, 2020 08:14
-
-
Save nilshartmann/1103b4df55479c5abf7ea51dbdbd85a8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function greet(person) { | |
// toUpperCase ist auf einem String definiert, aber nicht auf einem Objekt | |
return person.toUpperCase(); | |
} | |
greet({ | |
name: "Susi" | |
}) | |
// Laufzeitfehler: TypeError: p.toUpperCase is not a function |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let person = "Susi"; | |
console.log(typeof person); // Ausgabe: string | |
console.log(person.toUpperCase()); // Ausgabe: SUSI | |
person = 32; | |
console.log(typeof person); // Ausgabe: number | |
console.log(person + 1); // Ausgabe: 33 | |
person = function() { return "Kate" } | |
console.log(typeof person); // Ausgabe: function | |
console.log(person()); // Ausgabe: Kate |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from "react"; | |
function Hello(props) { | |
return <h1>Hello, {props.name}</h1> | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function greet(person: string) { | |
return person.toUpperCase(); | |
} | |
greet({ | |
name: "Susi" | |
}); | |
// TypeScript Fehler: Argument of type '{ name: string; }' | |
// is not assignable to parameter of type 'string'. | |
const greeting = greet("Susi"); // OK | |
greeting.toLowerCase(); // OK, greeting ist string und darauf ist die Funktion toLowerCase definiert. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let person = "Susi"; | |
console.log(typeof name); // Ausgabe: string | |
name = 32; // TypeScript Fehlermeldung: Type '32' is not assignable to type 'string' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Person = { name: string, lastname: string }; | |
function greet(person: Person) { | |
return `Hello, ${person.name} ${person.lastname}` | |
}; | |
greet({name: "Susi", lastname: "Mueller"}); // OK | |
greet({name: "Klaus"}); // TS Error: Property ‘lastname’ missing |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function greet(person: string | null) { | |
if (person === null) { | |
return ""; | |
} | |
return person.toUpperCase(); // OK | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function greet(person: string | null) { | |
return person.toUpperCase(); // TS Fehler: Object is possibly 'null' | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment