TypeScript/tests/cases/compiler/weakType.ts
Nathan Shively-Sanders 480b73915f
Improve excess property checking for intersections (#32582)
* Improve excess property checking for intersections

Still a draft, the implementation needs improvement

* Use mutable isIntersection in checkTypeRelatedTo

This makes parameter lists a lot shorter. Seems like a slight
improvement, although I can revert if I change my mind.

* Fix semicolon lint

* Remove TODOOOO

* Revert "Use mutable isIntersection in checkTypeRelatedTo"

This reverts commit b8dccff2a2.
2019-08-06 15:03:24 -07:00

64 lines
1.5 KiB
TypeScript

interface Settings {
timeout?: number;
onError?(): void;
}
function getDefaultSettings() {
return { timeout: 1000 };
}
interface CtorOnly {
new(s: string): { timeout: 1000 }
}
function doSomething(settings: Settings) { /* ... */ }
// forgot to call `getDefaultSettings`
doSomething(getDefaultSettings);
doSomething(() => ({ timeout: 1000 }));
doSomething(null as CtorOnly);
doSomething(12);
doSomething('completely wrong');
doSomething(false);
// this is an oddly popular way of defining settings
// this example is from services/textChanges.ts
type ConfigurableStart = { useStart?: boolean }
type ConfigurableEnd = { useEnd?: boolean }
type ConfigurableStartEnd = ConfigurableStart & ConfigurableEnd
interface InsertOptions {
prefix?: string
suffix?: string
}
type ChangeOptions = ConfigurableStartEnd & InsertOptions;
function del(options: ConfigurableStartEnd = {},
error: { error?: number } = {}) {
let changes: ChangeOptions[];
changes.push(options);
changes.push(error);
}
class K {
constructor(s: string) { }
}
// Ctor isn't a weak type because it has a construct signature
interface Ctor {
new (s: string): K
n?: number
}
let ctor: Ctor = K
type Spoiler = { nope?: string }
type Weak = {
a?: number
properties?: {
b?: number
}
}
declare let propertiesWrong: {
properties: {
wrong: string
}
}
let weak: Weak & Spoiler = propertiesWrong