📜  typescript as - TypeScript 代码示例

📅  最后修改于: 2022-03-11 14:48:21.191000             🧑  作者: Mango

代码示例1
// The 'as' keyword asserts that a variable has a type.                        =>
// TypeScript throws an error if these types have no overlap.

// The following example will work with jQuery.

let inputElt = $("#text_input");
// type of inputElt is JQuery

let inputValue = inputElt.val();
// type of inputValue is string | string[] | null | undefined

// If we are sure that the input element is a valid element and is only
// present once, the value must be of type string.

// Thus we can assert it is a string, as so:
let inputValueAsString = inputValue as string;

// If you want to learn more, click the link in the source.