Posted web鍓嶇寮€鍙?/a>
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了相关的知识,希望对你有一定的参考价值。
鑻辨枃 | https://www.sangle7.com/
1銆?nbsp;TypeScript 鍜?DOM
const textEl = document.querySelector('inpot');
console.log(textEl.value);
// 馃洃 Property 'value' does not exist on type 'Element'.
interface htmlElementTagNameMap {
a: HTMLAnchorElement;
abbr: HTMLElement;
address: HTMLElement;
applet: HTMLAppletElement;
area: HTMLAreaElement;
article: HTMLElement;
/* ... */
input: HTMLInputElement;
/* ... */
}
textEl.addEventListener('click', (e) => {
console.log(e.clientX);
});
2銆佹湡鏈涙硾鍨?/span>
document.querySelector锛?input.action'锛?/span>
textEl = document.querySelector < HTMLInputElement > 'input.action';
console.log(textEl.value);
// 馃憤 'value' is available because we've instructed TS
// about the type the 'querySelector' function works with.
3銆佲€滄垜浠湡鐨勬壘鍒颁簡鍚楋紵鈥?/span>
{
"compilerOptions": {
"strictNullChecks": true
}
}
const textEl = document.querySelector('input');
console.log(textEl!.value);
// 馃憤 with "!" we assure TypeScript
// that 'textEl' is not null/undefined
5銆佸綋杩佺Щ鍒?TS鈥?/span>
// tslint:disable
let mything = 2;
mything = 'hi';
// 馃洃 Type '"hi"' is not assignable to type 'number'
mything = 'hi' as any;
// 馃憤 if you say "any", TypeScript says 炉\_(銉?_/炉
6銆佹洿澶氶檺鍒?/span>
function fn(param) {
console.log(param);
}
{
"compilerOptions": {
"noImplicitAny": true
}
}
{
"rules": {
"typedef": [
true,
"call-signature"
]
}
}
7銆佺被鍨嬩繚鎶?/span>
type BookId = number | string;
function returnFormatterId(id: BookId) {
return id.toUpperCase();
// 馃洃 'toUpperCase' does not exist on type 'number'.
}
function returnFormatterId(id: BookId) {
if (typeof id === 'string') {
// we've made sure it's a string:
return id.toUpperCase(); // so it's 馃憤
}
// 馃憤 TS also understands that it
// has to be a number here:
return id.toFixed(2)
}
8銆佸啀璋堟硾鍨?/strong>
interface Bookmark {
id: string;
}
class BookmarksService {
items: Bookmark[] = [];
}
interface Movie {
id: string;
name: string;
}
class SearchPageComponent {
movie: Movie;
constructor(private bs: BookmarksService) {}
getFirstMovie() {
// 馃洃 types are not assignable
this.movie = this.bs.items[0];
// 馃憤 so you have to manually assert type:
this.movie = this.bs.items[0] as Movie;
}
getSecondMovie() {
this.movie = this.bs.items[1] as Movie;
}
}
class BookmarksService<T> {
items: T[] = [];
}
class BookmarksService<T extends Bookmark> {
items: T[] = [];
}
class SearchPageComponent {
movie: Movie;
constructor(private bs: BookmarksService<Movie>) {}
getFirstMovie() {
this.movie = this.bs.items[0]; // 馃憤
}
getSecondMovie() {
this.movie = this.bs.items[1]; // 馃憤
}
}
class BookmarksService<T extends Bookmark = Bookmark> {
items: T[] = [];
}
const bs = new BookmarksService();
// I don't have to provide the type for the generic now
// - in that case 'bs' will be of that default type 'Bookmark'
9銆佽矾鐢卞弬鏁?/span>
export interface DashboardRouteParams {
countryId: string;
deviceId: string;
}
const routes: Routes = [{
path: 'country/:countryId/device/:deviceId/dashboard'
}]
10銆佹牴鎹?API 鍝嶅簲鍒涘缓 Interface
-
http://www.json2ts.com -
http://www.jsontots.com
-
https://app.quicktype.io/
以上是关于的主要内容,如果未能解决你的问题,请参考以下文章