Python Shallow Copy: A New List Can Still Share Nested Data
Copying a list does not always make every object inside it independent. A shallow copy creates a new outer collection but keeps…
A value from outside your code may not have the shape you expect. TypeScript unknown lets you represent that uncertainty while requiring checks before type-specific operations. It does not automatically validate incoming data for you.
function readName(value: unknown): string {
if (typeof value !== "string") {
throw new Error("Name must be text");
}
return value.trim();
}
After the check, TypeScript understands that value is a string. With any, unsupported operations could pass the type checker without this evidence. The TypeScript Handbook explains unknown as a safer way to describe a value whose type has not yet been established.
An empty string still passes the string check. If a name must contain text after trimming, that needs another rule. Similarly, checking that an input is an object is not proof that all required fields exist or have valid values. Decide which conditions the feature actually needs.
Write the expected result for each before running the function. The object case should not be accepted merely because it looks related to the task.
For a Malta developer interview, describe where untrusted or uncertain data enters the application and how you establish its usable form. A type assertion alone is not evidence that a runtime value meets your requirements.
Explore software, cloud and data roles that fit your skills and ambitions.
Explore opportunitiesCopying a list does not always make every object inside it independent. A shallow copy creates a new outer collection but keeps…
A list item can move while still representing the same thing. React uses keys to identify items among their siblings. Choosing a…
A Python function with a list as its default argument can behave as though it remembers a previous call. The list is…