35
loading...
This website collects cookies to deliver better user experience
git clone https://github.com/kowalevski/ts-node-sample/tree/strict-mode-example
import { createQuestioner } from "./createQuestioner";
import { greeting } from "./greeting";
import usersDB from './users.json';
async function main() {
try {
const questioner = createQuestioner();
const username = await questioner.ask("Type your name: ");
const foundUser = usersDB.find((user) => user.username === username);
greeting(foundUser.username);
questioner.finishUp();
} catch (e) {
console.error(e);
process.exit();
}
}
main();
export function greeting(names) {
console.log(`Hello, ${names.map((name) => name.toLowerCase()).join(', ')}!`);
}
tsc && node dist/main.js
npm run dev
src/greeting.ts:1:26 - error TS7006: Parameter 'names' implicitly has an 'any' type.
1 export function greeting(names) {
~~~~~
src/greeting.ts:2:36 - error TS7006: Parameter 'name' implicitly has an 'any' type.
2 console.log(`Hello, ${names.map((name) => name.toLowerCase()).join(', ')}!`);
~~~~
Found 2 errors
export function greeting(names: string[]) {
console.log(`Hello, ${names.map((name) => name.toLowerCase()).join(', ')}!`);
}
src/main.ts:12:14 - error TS2345: Argument of type '{ username: string; age: number; } | undefined' is not assignable to parameter of type 'string[]'.
Type 'undefined' is not assignable to type 'string[]'.
12 greeting(foundUser.username);
~~~~~~~~~~~~~~~
Found 1 error
async function main() {
try {
const questioner = createQuestioner();
const username = await questioner.ask("Type your name: ");
const foundUser = usersDB.find((user) => user.username === username);
if (!foundUser) {
console.log('User is not found');
questioner.finishUp();
return;
}
greeting([foundUser.username]);
questioner.finishUp();
} catch (e) {
console.error(e);
process.exit();
}
}
npm run dev