[Typescript] Make Typescript Stick
作者:互联网
Refer: https://www.cnblogs.com/Answer1215/p/15084496.html
A string is a primitive value, and all primitive values are immutable.
Q1:
const a = "Frontend Masters"
let b = "Frontend Masters"
const c = { learnAt: "Frontend Masters" }
let d = { learnAt: "Frontend Masters" }
const e = Object.freeze({ learnAt: "Frontend Masters" })
.
.
.
.
.
.
Answer:
All primitive values are immutable, therefore a & b are both hold immutable values.
Repeat "All primitive values are immutable" three times...
All primitive values are immutable
All primitive values are immutable
All primitive values are immutable
Const and Let differ in terms of whether variables can be reassigned, but that has nothing to do with whether the values they hold can be modified.
Therefore both c & d does NOT hod ummutable values
Object.freeze prevents properties of an object from being changed, and prevents new properties from being added. This effectively is a "Shallow immutability". Which means Object.freeze won't help you mutate a nested object.
const e = Object.freeze({ course: 'Typescript', learnAt: {place: "Frontend Masters"} })
e.course = "Reacct" // report error
e.learnAt.place = "Udmey" // works
Q2:
const str = "hello"
let val =
/* ??? */
console.log(val)
/**
* {
* '0': 'h',
* '1': 'e',
* '2': 'l',
* '3': 'l',
* '4': 'o'
* }
*/
.
.
.
.
.
.
Answer:
let val = { ...str.split("") }
console.log(val)
/**
* {
* '0': 'h',
* '1': 'e',
* '2': 'l',
* '3': 'l',
* '4': 'o'
* }
*/
Q3:
Look at the types of
first
andsecond
below, as well as the compile error messages. What does your mental model tell you about howstring
andString
are different?let first: string & number
let second: String & Number
first = "abc"
Type 'string' is not assignable to type 'never'.
second = "abc"Type 'string' is not assignable to type 'String & Number'. Type 'string' is not assignable to type 'Number'.
second = new String("abc")Type 'String' is not assignable to type 'String & Number'. Type 'String' is missing the following properties from type 'Number': toFixed, toExponential, toPrecision
for the first: both string and number are primtive types, they are not such a value is both string and number at the same time, so that it results in a `never`.
for the second: String and Number are interface types. The Union of String and Number are common methods and props of both, but it won't results in a never type.
Q4:
In what order will the animal names below be printed to the console?
function getData() {
console.log("elephant")
const p = new Promise((resolve) => {
console.log("giraffe")
resolve("lion")
console.log("zebra")
})
console.log("koala")
return p
}
async function main() {
console.log("cat")
const result = await getData()
console.log(result)
}
console.log("dog")
main().then(() => {
console.log("moose")
})
.
.
.
.
.
.
.
Answer:
"dog" will come first
Then main() function will be invoked, so "cat" will come second.
Then getData() function will be invoked, so "elephant" will come third.
new Promise() constructor function will be invoked, 4th. "giraffe"
resolve() function will be invoked by the time we call .then()
so "zebra" comes 5th.
then "koala" comes 6th.
after await getData(), console.log(result) will run, so "lion" 7th.
"moose" will be last
- Are you surprised that
giraffe
andzebra
happen so early? Remember thatPromise
executors are invoked synchronously in thePromise
constructor - Are you surprised that
lion
happens so late? Remember that aresolve
is not areturn
. Just because aPromise
has resolved, doesn’t mean the corresponding.then
(orawait
is called immediately)
标签:Typescript,console,log,Make,will,values,Stick,string,String 来源: https://www.cnblogs.com/Answer1215/p/16464558.html