質問編集履歴

2

ロジック追記

2022/06/26 03:05

投稿

jin007
jin007

スコア34

test CHANGED
File without changes
test CHANGED
@@ -254,3 +254,38 @@
254
254
  }
255
255
  }
256
256
  ```
257
+
258
+
259
+
260
+ ------
261
+ ### 追記 6/26
262
+
263
+ index.ts
264
+ ```typescript
265
+ import World from './world';
266
+
267
+ const root: HTMLElement | null = document.getElementById("root")
268
+ const world = new World("Hello World!!!");
269
+ world.sayHello(root);
270
+ ```
271
+
272
+ ---
273
+ world.ts
274
+ ```typescript
275
+ export default class World {
276
+ // クラスで使うプロパティ
277
+ message: string;
278
+
279
+ // コンストラクタ(初期化)
280
+ constructor(message: string) {
281
+ this.message = message
282
+ }
283
+
284
+ // 外部から呼び出せるメソッドを定義
285
+ public sayHello(elem: HTMLElement | null) {
286
+ if (elem) {
287
+ elem.innerText = this.message
288
+ }
289
+ }
290
+ }
291
+ ```

1

ソースを追記

2022/06/24 07:43

投稿

jin007
jin007

スコア34

test CHANGED
File without changes
test CHANGED
@@ -82,5 +82,175 @@
82
82
   →[参考](https://insider.10bace.com/2017/11/29/typescript-ts18003-error/)
83
83
 
84
84
  ------
85
-
85
+ tsconfig.json
86
+ ```json
87
+ {
88
+ "compilerOptions": {
89
+ /* Visit https://aka.ms/tsconfig to read more about this file */
90
+
91
+ /* Projects */
92
+ // "incremental": true,
93
+ // "composite": true,
94
+ // "tsBuildInfoFile": "./.tsbuildinfo",
95
+ // "disableSourceOfProjectReferenceRedirect": true,
96
+ // "disableSolutionSearching": true,
97
+ // "disableReferencedProjectLoad": true,
98
+
99
+ /* Language and Environment */
100
+ "target": "es5",
101
+ // "lib": [],
102
+ // "jsx": "preserve",
103
+ // "experimentalDecorators": true,
104
+ // "emitDecoratorMetadata": true,
105
+ // "jsxFactory": "",
106
+ // "jsxFragmentFactory": "",
107
+ // "jsxImportSource": "",
108
+ // "reactNamespace": "",
109
+ // "noLib": true,
110
+ // "useDefineForClassFields": true,
111
+ // "moduleDetection": "auto",
112
+
113
+ /* Modules */
114
+ "module": "commonjs",
115
+ // "rootDir": "./",
116
+ // "moduleResolution": "node",
117
+ "baseUrl": "./",
118
+ // "paths": {},
119
+ // "rootDirs": [],
120
+ // "typeRoots": [],
121
+ // "types": [],
122
+ // "allowUmdGlobalAccess": true,
123
+ // "moduleSuffixes": [],
124
+ // "resolveJsonModule": true,
125
+ // "noResolve": true,
126
+
127
+ /* JavaScript Support */
128
+ "allowJs": true,
129
+ // "checkJs": true,
130
+ // "maxNodeModuleJsDepth": 1,
131
+
132
+ /* Emit */
133
+ // "declaration": true,
134
+ // "declarationMap": true,
135
+ // "emitDeclarationOnly": true,
136
+ // "sourceMap": true,
137
+ // "outFile": "./",
138
+ "outDir": "./",
139
+ // "removeComments": true,
140
+ // "noEmit": true,
141
+ // "importHelpers": true,
142
+ // "importsNotUsedAsValues": "remove",
143
+ // "downlevelIteration": true,
144
+ // "sourceRoot": "",
145
+ // "mapRoot": "",
146
+ // "inlineSourceMap": true,
147
+ // "inlineSources": true,
148
+ // "emitBOM": true,
149
+ // "newLine": "crlf",
150
+ // "stripInternal": true,
151
+ // "noEmitHelpers": true,
152
+ // "noEmitOnError": true,
153
+ // "preserveConstEnums": true,
154
+ // "declarationDir": "./",
155
+ // "preserveValueImports": true,
156
+
157
+ /* Interop Constraints */
158
+ // "isolatedModules": true,
159
+ // "allowSyntheticDefaultImports": true,
160
+ "esModuleInterop": true,
161
+ // "preserveSymlinks": true,
162
+ "forceConsistentCasingInFileNames": true,
163
+
164
+ /* Type Checking */
165
+ "strict": true,
166
+ // "noImplicitAny": true,
167
+ // "strictNullChecks": true,
168
+ // "strictFunctionTypes": true,
169
+ // "strictBindCallApply": true,
170
+ // "strictPropertyInitialization": true,
171
+ // "noImplicitThis": true,
172
+ // "useUnknownInCatchVariables": true,
173
+ // "alwaysStrict": true,
174
+ // "noUnusedLocals": true,
175
+ // "noUnusedParameters": true,
176
+ // "exactOptionalPropertyTypes": true,
177
+ // "noImplicitReturns": true,
178
+ // "noFallthroughCasesInSwitch": true,
179
+ // "noUncheckedIndexedAccess": true,
180
+ // "noImplicitOverride": true,
181
+ // "noPropertyAccessFromIndexSignature": true,
182
+ // "allowUnusedLabels": true,
183
+ // "allowUnreachableCode": true,
184
+
185
+ /* Completeness */
186
+ // "skipDefaultLibCheck": true,
187
+ "skipLibCheck": true
188
+ }
189
+ }
190
+ ```
191
+
192
+ ---
193
+ package.json
194
+ ```json
195
+ {
196
+ "name": "y",
197
+ "version": "1.0.0",
198
+ "description": "",
199
+ "main": "index.js",
200
+ "scripts": {
201
+ "build": "webpack --mode=production",
202
+ "start": "webpack-cli serve --mode development"
203
+ },
204
+ "author": "",
205
+ "license": "ISC",
206
+ "devDependencies": {
207
+ "ts-loader": "^9.3.1",
208
+ "typescript": "^4.7.4",
209
+ "webpack": "^5.73.0",
210
+ "webpack-cli": "^4.10.0",
211
+ "webpack-dev-server": "^4.9.2"
212
+ }
213
+ }
214
+ ```
215
+ ---
216
+ webpack.js
217
+ ```js
218
+ const path = require('path');
86
- **文字数の都合上ソースは後述**
219
+ module.exports = {
220
+ // モジュールバンドルを行う起点となるファイルの指定
221
+ // 指定できる値としては、ファイル名の文字列や、それを並べた配列やオブジェクト
222
+ // 下記はオブジェクトとして指定した例
223
+ entry: {
224
+ bundle: './src/index.ts'
225
+ },
226
+ // モジュールバンドルを行った結果を出力する場所やファイル名の指定
227
+ output: {
228
+ path: path.join(__dirname, 'dist'), // "__dirname"はファイルが存在するディレクトリ
229
+ filename: '[name].js' // [name]はentryで記述した名前(この設定ならbundle)
230
+ },
231
+ // import文でファイル拡張子を書かずに名前解決するための設定
232
+ // 例...「import World from './world'」と記述すると"world.ts"という名前のファイルをモジュールとして探す
233
+ resolve: {
234
+ extensions:['.ts','.js'] // Reactの.tsxや.jsxの拡張子も扱いたい場合は配列内に追加する
235
+ },
236
+ devServer: {
237
+ //contentBase: path.join(__dirname, 'dist'), // webpack-dev-serverの公開フォルダ
238
+ // ↑現在「contentBase」の使用は不可になっており、代わりにstaticを使って記述することになっている。
239
+ static:{
240
+ directory: path.join(__dirname, 'dist'),
241
+ //open: true // サーバー起動時にブラウザを開く
242
+ }
243
+ },
244
+ // モジュールに適用するルールの設定(ローダーの設定を行う事が多い)
245
+ module: {
246
+ rules: [
247
+ {
248
+ // 拡張子が.tsのファイルに対してTypeScriptコンパイラを適用する
249
+ // Reactで用いる.tsxの拡張子にも適用する場合は test:/\.(ts|tsx)$/,
250
+ test: /\.ts$/,
251
+ loader: 'ts-loader'
252
+ }
253
+ ]
254
+ }
255
+ }
256
+ ```