mirror of
https://github.com/vercel-labs/scriptc.git
synced 2026-11-03 07:54:40 +08:00
- An empty `[]` whose own type is `never[]` (a `return []` in an async function, whose contextual type is `T[] | PromiseLike<T[]>`, or an object literal's `files: []` field) builds as the unit-only element array, like the empty tuple, so typed array slots receive it through the checked empty-array lift. It was `number[]`, so `string[]` slots refused it with SC1090 or SC2002. - The refusal "instanceof against this native constructor" names its site, and coverage lists the sites whose right-hand side is `Object` or `Function`, which always reach it. An arrow function or method on the right of an untyped `instanceof` throws Node's "Function has non-object prototype" TypeError for an object. - Coverage lists the diagnostics of a function whose signature did not compile. A use of a parameter whose type did not compile reports SC2004. - Coverage lists deferred fences in a module's top-level code under "runs at startup", and the diagnostics envelope marks them `startup: true`.
59 lines
1.5 KiB
TypeScript
59 lines
1.5 KiB
TypeScript
// Empty array literals whose own type is `never[]`: `return []` in an async
|
|
// function (its contextual type is `string[] | PromiseLike<string[]>`) and
|
|
// an object literal's `files: []` field returned or passed where typed
|
|
// arrays are declared. Each value holds the declared element type.
|
|
interface Listing {
|
|
files: string[];
|
|
sizes: number[];
|
|
}
|
|
|
|
async function readNames(text: string): Promise<string[]> {
|
|
try {
|
|
return JSON.parse(text) as string[];
|
|
} catch {
|
|
return [];
|
|
}
|
|
}
|
|
|
|
const loadIds = async (fail: boolean): Promise<number[]> => {
|
|
if (fail) return [];
|
|
return [1, 2];
|
|
};
|
|
|
|
function emptyListing(): Listing {
|
|
const listing = { files: [], sizes: [] };
|
|
return listing;
|
|
}
|
|
|
|
function index(): Record<"files" | "dirs", string[]> {
|
|
const out = { files: [], dirs: [] };
|
|
return out;
|
|
}
|
|
|
|
function total(listing: Listing): number {
|
|
return listing.sizes.reduce((sum, size) => sum + size, 0);
|
|
}
|
|
|
|
async function main(): Promise<void> {
|
|
const names = await readNames("not json");
|
|
names.push("fallback");
|
|
console.log(names, (await readNames('["a","b"]')).join("+"));
|
|
const ids = await loadIds(true);
|
|
ids.push(7);
|
|
console.log(ids, await loadIds(false));
|
|
|
|
const listing = emptyListing();
|
|
listing.files.push("a.txt");
|
|
listing.sizes.push(3, 4);
|
|
console.log(listing, total(listing));
|
|
|
|
const dirs = index();
|
|
dirs.dirs.push("src");
|
|
console.log(JSON.stringify(dirs), dirs.files.length);
|
|
|
|
const blank = { files: [], sizes: [] };
|
|
console.log(total(blank), JSON.stringify(blank));
|
|
}
|
|
|
|
main();
|