JS/TSオタクです
2020/03/18時点では/v2/
つけるとアクセスできそう
https://www.typescriptlang.org/v2/dev/playground-plugins/
Options
タブ開くts-playground-plugin-stackoverflow
と入力playgroundのエラーと類似のエラーをStackoverflowから探してサジェストするplaygroundプラグイン。
精度はご愛嬌... 🙏
TypeScript playground plugin
TypeScript Compiler API/LanguageService
Monaco editor
Stackexchange API
TypeScript playground plugin (サブ)
TypeScript Compiler API/LanguageService (メイン)
Monaco editor (割愛)
Stackexchange API (割愛)
プラグイン機構や使えるAPI、フックなど
https://www.typescriptlang.org/v2/dev/playground-plugins/
yarn create typescript-playground-plugin xxx
cd xxx
yarn start
このリポジトリもテンプレから作ってる:
Leko/ts-playground-plugin-stackoverflow
import type { PlaygroundPlugin, PluginUtils } from "./vendor/playground"
export default (utils: PluginUtils): PlaygroundPlugin => {
return {
id: 'xxx',
displayName: 'タブの名前',
didMount(sandbox, container) {
// ...
},
modelChangedDebounce(sandbox, model) {
// ...
},
}
}
didMount
modelChanged(Debounce)
これ以外のフックは生成されたPlaygroundPlugin
の型定義を参照
コードに対して何か(型チェック、エラーの整形)して画面に表示する
コードが変更されたとき
export interface DiagnosticRelatedInformation {
category: DiagnosticCategory;
code: number;
file: SourceFile | undefined;
// コード上のエラー開始位置
start: number | undefined;
// エラーが起こっているコードの長さ
length: number | undefined;
// エラーメッセージ OR ネストしたエラー
messageText: string | DiagnosticMessageChain;
}
export interface Diagnostic extends DiagnosticRelatedInformation {
/** ... */
reportsUnnecessary?: {};
source?: string;
relatedInformation?: DiagnosticRelatedInformation[];
}
1: const port = process.env.PORT;
^^^^^^^
↑start, lengthから該当行、エラー箇所を抽出(コード)
error TS2580: Cannot find name 'process'. Do you need to install type definitions for node? Try
`npm i @types/node`.
↑Compiler APIの一部を使用
messageText
といいつつstringではない messageText: string | DiagnosticMessageChain;
import type { FormatDiagnosticsHost } from 'typescript'
const formatDiagnosticHost: FormatDiagnosticsHost = { /* ... */ }
const errorMessage = ts.formatDiagnostic(d, formatDiagnosticHost)
FormatDiagnosticsHost... 🤔
~Host
はTSをプラットフォーム非依存するためのDIdeclare namespace ts {
// ...
export interface FormatDiagnosticsHost {
getCurrentDirectory(): string;
getCanonicalFileName(fileName: string): string;
getNewLine(): string;
}
const formatDiagnosticHost: FormatDiagnosticsHost = {
getCurrentDirectory() {
// playgroundにディレクトリの概念はないので型だけ合わせる
return ''
},
getCanonicalFileName(fileName: string) {
// ファイルパスのエスケープもないのでそのまま返す
return fileName
},
getNewLine() {
return '\n'
}
}
Q. 何をどうやって勉強したらいいですか
Q. 画面への反映ってどうしてますか
Q. 生DOM触るのつらくないですか