Upgrade to Pro — share decks privately, control downloads, hide ads and more …

tsconfigのオプションで変わる型世界

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.

 tsconfigのオプションで変わる型世界

Avatar for Keisuke Ikeda

Keisuke Ikeda

May 23, 2025
Tweet

Other Decks in Programming

Transcript

  1. tsconfigに向き合おう! 05/16 ͜ͷൃදͰߦ͏͜ͱ ɾॳ৺ऀ޲͚ʹֶͿɾݟ௚ͨ͢Ίͷ͖͔͚ͬΛఏڙ ɾ۩ମతʹ͸ʁ ɹ- ॳظઃఆʢtsc --initʣͷ tsconfig.json ΛϕʔεʹɺҰ෦ͷΦϓγϣϯ

    ɹɹΛมߋ͠ɺ͋͑ͯʮܕʯΛ؇࿨ ɹ- ઃఆͷมߋͰܕ҆શੑ͕Ͳ͏มΘΔ͔Λݕূ ɾʮܕͷ͋Γ͕ͨΈʯͱʮઃఆͷॏཁੑʯΛ࣮ײ
  2. 45&1ɿॳظঢ়ଶ index.ts オプションによる変化 08/16 type User = { id: number;

    name: string; }; class UserManager { // TS2564:プロパティが未初期化 private users: User[]; // TS7006:引数が暗黙的にany型 addUser(user) { this.users.push(user); return user.id; } findUserById(id: number): User | null { const foundUser = this.users.find((user) => user.id === id); // TS2322:型の不一致 "User | undefined"型 return foundUser; } getUserName(id: number): string { const user = this.findUserById(id); // TS18047:usersがnullable return user.name; } } tsconfig.json { "compilerOptions": { "target": "es2016", "module": "commonjs", "esModuleInterop": true, "strict": true, "skipLibCheck": true, "outDir": "./dist" }, "include": ["src/*.ts"], "exclude": ["node_modules"] }
  3. 45&1ɿॳظঢ়ଶ index.ts オプションによる変化 09/16 type User = { id: number;

    name: string; }; class UserManager { // TS2564:プロパティが未初期化 private users: User[]; // TS7006:引数が暗黙的にany型 addUser(user) { this.users.push(user); return user.id; } findUserById(id: number): User | null { const foundUser = this.users.find((user) => user.id === id); // TS2322:型の不一致 "User | undefined"型 return foundUser; } getUserName(id: number): string { const user = this.findUserById(id); // TS18047:usersがnullable return user.name; } } tsconfig.json { "compilerOptions": { "target": "es2016", "module": "commonjs", "esModuleInterop": true, "strict": true, "skipLibCheck": true, "outDir": "./dist" }, "include": ["src/*.ts"], "exclude": ["node_modules"] } 54ϓϩύςΟ͕ະॳظԽ 54Ҿ਺͕҉໧తʹBOZܕ 54ܕͷෆҰகʢOVMMͱVOEFGJOFEͷ۠ผʣ 54ฦΓ஋͕OVMMBCMF
  4. 45&1ɿϓϩύςΟॳظԽνΣοΫແޮ index.ts オプションによる変化 11/16 type User = { id: number;

    name: string; }; class UserManager { // TS2564:プロパティが未初期化 private users: User[]; // TS7006:引数が暗黙的にany型 addUser(user) { this.users.push(user); return user.id; } findUserById(id: number): User | null { const foundUser = this.users.find((user) => user.id === id); // TS2322:型の不一致 "User | undefined"型 return foundUser; } getUserName(id: number): string { const user = this.findUserById(id); // TS18047:usersがnullable return user.name; } } tsconfig.json { "compilerOptions": { "target": "es2016", "module": "commonjs", "esModuleInterop": true, "strict": true, + "strictPropertyInitialization": false, "skipLibCheck": true, "outDir": "./dist" }, "include": ["src/*.ts"], "exclude": ["node_modules"] } Τϥʔ࡟আ
  5. 45&1ɿOVMMͱVOEFGJOFEͷݫີͳ۠ผˍνΣοΫແޮ index.ts オプションによる変化 12/16 type User = { id: number;

    name: string; }; class UserManager { // TS2564:プロパティが未初期化 private users: User[]; // TS7006:引数が暗黙的にany型 addUser(user) { this.users.push(user); return user.id; } findUserById(id: number): User | null { const foundUser = this.users.find((user) => user.id === id); // TS2322:型の不一致 "User | undefined"型 return foundUser; } getUserName(id: number): string { const user = this.findUserById(id); // TS18047:usersがnullable return user.name; } } tsconfig.json { "compilerOptions": { "target": "es2016", "module": "commonjs", "esModuleInterop": true, "strict": true, "strictPropertyInitialization": false, + "strictNullChecks": false, "skipLibCheck": true, "outDir": "./dist" }, "include": ["src/*.ts"], "exclude": ["node_modules"] } Τϥʔ࡟আ Τϥʔ࡟আ
  6. 45&1ɿ҉໧తͳBOZܕͷνΣοΫແޮ index.ts オプションによる変化 13/16 type User = { id: number;

    name: string; }; class UserManager { // TS2564:プロパティが未初期化 private users: User[]; // TS7006:引数が暗黙的にany型 addUser(user) { this.users.push(user); return user.id; } findUserById(id: number): User | null { const foundUser = this.users.find((user) => user.id === id); // TS2322:型の不一致 "User | undefined"型 return foundUser; } getUserName(id: number): string { const user = this.findUserById(id); // TS18047:usersがnullable return user.name; } } tsconfig.json { "compilerOptions": { "target": "es2016", "module": "commonjs", "esModuleInterop": true, "strict": true, "strictPropertyInitialization": false, "strictNullChecks": false, + "noImplicitAny": false, "skipLibCheck": true, "outDir": "./dist" }, "include": ["src/*.ts"], "exclude": ["node_modules"] } Τϥʔ࡟আ ίϯύΠϧ࣌ͷΤϥʔ͕શͯফ͑ͯ͠·ͬͨ…
  7. ࣮ߦͰ͖ͳ͍ϓϩάϥϜ index.ts オプションによる変化 14/16 const manager = new UserManager(); //

    strictPropertyInitialization:falseにより見落とされるエラー // 実行時エラー: Cannot read properties of undefined (reading 'push') const user = { id: 1, name: '山田太郎' }; manager.addUser(user); // strictNullChecks:falseにより見落とされるエラー // 実行時エラー: Cannot read properties of undefined (reading 'find') const nonExistentUser = manager.findUserById(999); // noImplicitAny:falseにより見落とされる不具合 // 実行エラーにもならず許容されてしまう。 const incompleteUser = { name: true }; manager.addUser(incompleteUser);