file_path
stringlengths
3
280
file_language
stringclasses
66 values
content
stringlengths
1
1.04M
repo_name
stringlengths
5
92
repo_stars
int64
0
154k
repo_description
stringlengths
0
402
repo_primary_language
stringclasses
108 values
developer_username
stringlengths
1
25
developer_name
stringlengths
0
30
developer_company
stringlengths
0
82
crates/swc_ecma_parser/tests/tsc/contextualTypedSpecialAssignment.ts
TypeScript
// @checkJs: true // @allowJs: true // @noEmit: true // @Filename: test.js // @strict: true /** @typedef {{ status: 'done' m(n: number): void }} DoneStatus */ // property assignment var ns = {} /** @type {DoneStatus} */ ns.x = { status: 'done', m(n) { } } ns.x = { status: 'done', m(n) { } } ns.x // this-property assignment class Thing { constructor() { /** @type {DoneStatus} */ this.s = { status: 'done', m(n) { } } } fail() { this.s = { status: 'done', m(n) { } } } } // exports-property assignment /** @type {DoneStatus} */ exports.x = { status: "done", m(n) { } } exports.x /** @type {DoneStatus} */ module.exports.y = { status: "done", m(n) { } } module.exports.y // prototype-property assignment /** @type {DoneStatus} */ Thing.prototype.x = { status: 'done', m(n) { } } Thing.prototype.x // prototype assignment function F() { } /** @type {DoneStatus} */ F.prototype = { status: "done", m(n) { } } // @Filename: mod.js // module.exports assignment /** @type {{ status: 'done', m(n: number): void }} */ module.exports = { status: "done", m(n) { } }
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/contextuallyTypeAsyncFunctionAwaitOperand.ts
TypeScript
// @target: esnext // @noImplicitAny: true // @noEmit: true interface Obj { key: "value"; } async function fn1(): Promise<Obj> { const obj1: Obj = await { key: "value" }; const obj2: Obj = await new Promise(resolve => resolve({ key: "value" })); return await { key: "value" }; }
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/contextuallyTypeAsyncFunctionReturnType.ts
TypeScript
// @target: esnext // @noImplicitAny: true // @noEmit: true interface Obj { key: "value"; } async function fn1(): Promise<Obj> { return { key: "value" }; } async function fn2(): Promise<Obj> { return new Promise(resolve => { resolve({ key: "value" }); }); } async function fn3(): Promise<Obj> { return await { key: "value" }; } async function fn4(): Promise<Obj> { return await new Promise(resolve => { resolve({ key: "value" }); }); } declare class Context { private _runnable; } type Done = (err?: any) => void; type Func = (this: Context, done: Done) => void; type AsyncFunc = (this: Context) => PromiseLike<any>; interface TestFunction { (fn: Func): void; (fn: AsyncFunc): void; (title: string, fn?: Func): void; (title: string, fn?: AsyncFunc): void; } declare const test: TestFunction; interface ProcessTreeNode { pid: number; name: string; memory?: number; commandLine?: string; children: ProcessTreeNode[]; } export declare function getProcessTree( rootPid: number, callback: (tree: ProcessTreeNode) => void ): void; test("windows-process-tree", async () => { return new Promise((resolve, reject) => { getProcessTree(123, (tree) => { if (tree) { resolve(); } else { reject(new Error("windows-process-tree")); } }); }); }); interface ILocalExtension { isApplicationScoped: boolean; publisherId: string | null; } type Metadata = { updated: boolean; }; declare function scanMetadata( local: ILocalExtension ): Promise<Metadata | undefined>; async function copyExtensions( fromExtensions: ILocalExtension[] ): Promise<void> { const extensions: [ILocalExtension, Metadata | undefined][] = await Promise.all( fromExtensions .filter((e) => !e.isApplicationScoped) .map(async (e) => [e, await scanMetadata(e)]) ); }
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/contextuallyTypeCommaOperator01.ts
TypeScript
// @allowUnreachableCode: true // @noImplicitAny: true let x: (a: string) => string; x = (100, a => a);
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/contextuallyTypeCommaOperator02.ts
TypeScript
// @allowUnreachableCode: true // @noImplicitAny: true let x: (a: string) => string; x = (100, a => { const b: number = a; return b; });
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/contextuallyTypeCommaOperator03.ts
TypeScript
// @allowUnreachableCode: true // @noImplicitAny: true let x: (a: string) => string; x = (a => a, b => b);
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/contextuallyTypeLogicalAnd01.ts
TypeScript
// @noImplicitAny: true let x: (a: string) => string; let y = true; x = y && (a => a);
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/contextuallyTypeLogicalAnd02.ts
TypeScript
// @noImplicitAny: true let x: (a: string) => string; let y = true; x = y && (a => { const b: number = a; return b; });
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/contextuallyTypeLogicalAnd03.ts
TypeScript
// @noImplicitAny: true let x: (a: string) => string; let y = true; x = (a => a) && (b => b);
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/contextuallyTypedBindingInitializer.ts
TypeScript
// @noImplicitAny: true interface Show { show: (x: number) => string; } function f({ show = v => v.toString() }: Show) {} function f2({ "show": showRename = v => v.toString() }: Show) {} function f3({ ["show"]: showRename = v => v.toString() }: Show) {} interface Nested { nested: Show } function ff({ nested = { show: v => v.toString() } }: Nested) {} interface Tuples { prop: [string, number]; } function g({ prop = ["hello", 1234] }: Tuples) {} interface StringUnion { prop: "foo" | "bar"; } function h({ prop = "foo" }: StringUnion) {} interface StringIdentity { stringIdentity(s: string): string; } let { stringIdentity: id = arg => arg }: StringIdentity = { stringIdentity: x => x};
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/contextuallyTypedBindingInitializerNegative.ts
TypeScript
// @noImplicitAny: true interface Show { show: (x: number) => string; } function f({ show: showRename = v => v }: Show) {} function f2({ "show": showRename = v => v }: Show) {} function f3({ ["show"]: showRename = v => v }: Show) {} interface Nested { nested: Show } function ff({ nested: nestedRename = { show: v => v } }: Nested) {} interface StringIdentity { stringIdentity(s: string): string; } let { stringIdentity: id = arg => arg.length }: StringIdentity = { stringIdentity: x => x}; interface Tuples { prop: [string, number]; } function g({ prop = [101, 1234] }: Tuples) {} interface StringUnion { prop: "foo" | "bar"; } function h({ prop = "baz" }: StringUnion) {}
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/contextuallyTypedClassExpressionMethodDeclaration01.ts
TypeScript
// @noImplicitAny: true interface A { numProp: number; } interface B { strProp: string; } interface Foo { method1(arg: A): void; method2(arg: B): void; } function getFoo1(): Foo { return class { static method1(arg) { arg.numProp = 10; } static method2(arg) { arg.strProp = "hello"; } } } function getFoo2(): Foo { return class { static method1 = (arg) => { arg.numProp = 10; } static method2 = (arg) => { arg.strProp = "hello"; } } } function getFoo3(): Foo { return class { static method1 = function (arg) { arg.numProp = 10; } static method2 = function (arg) { arg.strProp = "hello"; } } }
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/contextuallyTypedClassExpressionMethodDeclaration02.ts
TypeScript
// @noImplicitAny: true interface A { numProp: number; } interface B { strProp: string; } interface Foo { new (): Bar; } interface Bar { method1(arg: A): void; method2(arg: B): void; } function getFoo1(): Foo { return class { method1(arg) { arg.numProp = 10; } method2(arg) { arg.strProp = "hello"; } } } function getFoo2(): Foo { return class { method1 = (arg) => { arg.numProp = 10; } method2 = (arg) => { arg.strProp = "hello"; } } } function getFoo3(): Foo { return class { method1 = function (arg) { arg.numProp = 10; } method2 = function (arg) { arg.strProp = "hello"; } } }
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/contextuallyTypedFunctionExpressionsAndReturnAnnotations.ts
TypeScript
declare function foo(x: (y: string) => (y2: number) => void); // Contextually type the parameter even if there is a return annotation foo((y): (y2: number) => void => { var z = y.charAt(0); // Should be string return null; }); foo((y: string) => { return y2 => { var z = y2.toFixed(); // Should be string return 0; }; });
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/contextuallyTypedIife.ts
TypeScript
// arrow (jake => { })("build"); // function expression (function (cats) { })("lol"); // Lots of Irritating Superfluous Parentheses (function (x) { } ("!")); ((((function (y) { }))))("-"); // multiple arguments ((a, b, c) => { })("foo", 101, false); // default parameters ((m = 10) => m + 1)(12); ((n = 10) => n + 1)(); // optional parameters ((j?) => j + 1)(12); ((k?) => k + 1)(); ((l, o?) => l + o)(12); // o should be any // rest parameters ((...numbers) => numbers.every(n => n > 0))(5,6,7); ((...mixed) => mixed.every(n => !!n))(5,'oops','oh no'); ((...noNumbers) => noNumbers.some(n => n > 0))(); ((first, ...rest) => first ? [] : rest.map(n => n > 0))(8,9,10); // destructuring parameters (with defaults too!) (({ q }) => q)({ q : 13 }); (({ p = 14 }) => p)({ p : 15 }); (({ r = 17 } = { r: 18 }) => r)({r : 19}); (({ u = 22 } = { u: 23 }) => u)(); // contextually typed parameters. let twelve = (f => f(12))(i => i); let eleven = (o => o.a(11))({ a: function(n) { return n; } }); // missing arguments (function(x, undefined) { return x; })(42); ((x, y, z) => 42)();
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/contextuallyTypedIifeStrict.ts
TypeScript
// @strictNullChecks: true // arrow (jake => { })("build"); // function expression (function (cats) { })("lol"); // Lots of Irritating Superfluous Parentheses (function (x) { } ("!")); ((((function (y) { }))))("-"); // multiple arguments ((a, b, c) => { })("foo", 101, false); // default parameters ((m = 10) => m + 1)(12); ((n = 10) => n + 1)(); // optional parameters ((j?) => j + 1)(12); ((k?) => k + 1)(); ((l, o?) => l + o)(12); // rest parameters ((...numbers) => numbers.every(n => n > 0))(5,6,7); ((...mixed) => mixed.every(n => !!n))(5,'oops','oh no'); ((...noNumbers) => noNumbers.some(n => n > 0))(); ((first, ...rest) => first ? [] : rest.map(n => n > 0))(8,9,10); // destructuring parameters (with defaults too!) (({ q }) => q)({ q : 13 }); (({ p = 14 }) => p)({ p : 15 }); (({ r = 17 } = { r: 18 }) => r)({r : 19}); (({ u = 22 } = { u: 23 }) => u)(); // contextually typed parameters. let twelve = (f => f(12))(i => i); let eleven = (o => o.a(11))({ a: function(n) { return n; } }); // missing arguments (function(x, undefined) { return x; })(42); ((x, y, z) => 42)();
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/contextuallyTypedObjectLiteralMethodDeclaration01.ts
TypeScript
// @noImplicitAny: true interface A { numProp: number; } interface B { strProp: string; } interface Foo { method1(arg: A): void; method2(arg: B): void; } function getFoo1(): Foo { return { method1(arg) { arg.numProp = 10; }, method2(arg) { arg.strProp = "hello"; } } } function getFoo2(): Foo { return { method1: (arg) => { arg.numProp = 10; }, method2: (arg) => { arg.strProp = "hello"; } } } function getFoo3(): Foo { return { method1: function (arg) { arg.numProp = 10; }, method2: function (arg) { arg.strProp = "hello"; } } }
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/contextuallyTypedStringLiteralsInJsxAttributes01.tsx
TypeScript (TSX)
// @jsx: preserve // @declaration: true namespace JSX { export interface IntrinsicElements { span: {}; } export interface Element { something?: any; } } const FooComponent = (props: { foo: "A" | "B" | "C" }) => <span>{props.foo}</span>; <FooComponent foo={"A"} />; <FooComponent foo="A" />; <FooComponent foo={"f"} />; <FooComponent foo="f" />;
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/contextuallyTypedStringLiteralsInJsxAttributes02.tsx
TypeScript (TSX)
// @filename: file.tsx // @jsx: preserve // @module: amd // @noLib: true // @skipLibCheck: true // @libFiles: react.d.ts,lib.d.ts import React = require('react') export interface ClickableProps { children?: string; className?: string; } export interface ButtonProps extends ClickableProps { onClick: (k: "left" | "right") => void; } export interface LinkProps extends ClickableProps { goTo: "home" | "contact"; } export function MainButton(buttonProps: ButtonProps): JSX.Element; export function MainButton(linkProps: LinkProps): JSX.Element; export function MainButton(props: ButtonProps | LinkProps): JSX.Element { const linkProps = props as LinkProps; if(linkProps.goTo) { return this._buildMainLink(props); } return this._buildMainButton(props); } const b0 = <MainButton {...{onClick: (k) => {console.log(k)}}} extra />; // k has type "left" | "right" const b2 = <MainButton onClick={(k)=>{console.log(k)}} extra />; // k has type "left" | "right" const b3 = <MainButton {...{goTo:"home"}} extra />; // goTo has type"home" | "contact" const b4 = <MainButton goTo="home" extra />; // goTo has type "home" | "contact" export function NoOverload(buttonProps: ButtonProps): JSX.Element { return undefined } const c1 = <NoOverload {...{onClick: (k) => {console.log(k)}}} extra />; // k has type any export function NoOverload1(linkProps: LinkProps): JSX.Element { return undefined } const d1 = <NoOverload1 {...{goTo:"home"}} extra />; // goTo has type "home" | "contact"
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/controlFlowAliasing.ts
TypeScript
// @strict: true // @declaration: true // Narrowing by aliased conditional expressions function f10(x: string | number) { const isString = typeof x === "string"; if (isString) { let t: string = x; } else { let t: number = x; } } function f11(x: unknown) { const isString = typeof x === "string"; if (isString) { let t: string = x; } } function f12(x: string | number | boolean) { const isString = typeof x === "string"; const isNumber = typeof x === "number"; if (isString || isNumber) { let t: string | number = x; } else { let t: boolean = x; } } function f13(x: string | number | boolean) { const isString = typeof x === "string"; const isNumber = typeof x === "number"; const isStringOrNumber = isString || isNumber; if (isStringOrNumber) { let t: string | number = x; } else { let t: boolean = x; } } function f14(x: number | null | undefined): number | null { const notUndefined = x !== undefined; return notUndefined ? x : 0; } function f15(obj: { readonly x: string | number }) { const isString = typeof obj.x === 'string'; if (isString) { let s: string = obj.x; } } function f16(obj: { readonly x: string | number }) { const isString = typeof obj.x === 'string'; obj = { x: 42 }; if (isString) { let s: string = obj.x; // Not narrowed because of is assigned in function body } } function f17(obj: readonly [string | number]) { const isString = typeof obj[0] === 'string'; if (isString) { let s: string = obj[0]; } } function f18(obj: readonly [string | number]) { const isString = typeof obj[0] === 'string'; obj = [42]; if (isString) { let s: string = obj[0]; // Not narrowed because of is assigned in function body } } function f20(obj: { kind: 'foo', foo: string } | { kind: 'bar', bar: number }) { const isFoo = obj.kind === 'foo'; if (isFoo) { obj.foo; } else { obj.bar; } } function f21(obj: { kind: 'foo', foo: string } | { kind: 'bar', bar: number }) { const isFoo: boolean = obj.kind === 'foo'; if (isFoo) { obj.foo; // Not narrowed because isFoo has type annotation } else { obj.bar; // Not narrowed because isFoo has type annotation } } function f22(obj: { kind: 'foo', foo: string } | { kind: 'bar', bar: number }) { let isFoo = obj.kind === 'foo'; if (isFoo) { obj.foo; // Not narrowed because isFoo is mutable } else { obj.bar; // Not narrowed because isFoo is mutable } } function f23(obj: { kind: 'foo', foo: string } | { kind: 'bar', bar: number }) { const isFoo = obj.kind === 'foo'; obj = obj; if (isFoo) { obj.foo; // Not narrowed because obj is assigned in function body } else { obj.bar; // Not narrowed because obj is assigned in function body } } function f24(arg: { kind: 'foo', foo: string } | { kind: 'bar', bar: number }) { const obj = arg; const isFoo = obj.kind === 'foo'; if (isFoo) { obj.foo; } else { obj.bar; } } function f25(arg: { kind: 'foo', foo: string } | { kind: 'bar', bar: number }) { let obj = arg; const isFoo = obj.kind === 'foo'; if (isFoo) { obj.foo; } else { obj.bar; } } function f26(outer: { readonly obj: { kind: 'foo', foo: string } | { kind: 'bar', bar: number } }) { const isFoo = outer.obj.kind === 'foo'; if (isFoo) { outer.obj.foo; } else { outer.obj.bar; } } function f27(outer: { obj: { kind: 'foo', foo: string } | { kind: 'bar', bar: number } }) { const isFoo = outer.obj.kind === 'foo'; if (isFoo) { outer.obj.foo; // Not narrowed because obj is mutable } else { outer.obj.bar; // Not narrowed because obj is mutable } } function f28(obj?: { kind: 'foo', foo: string } | { kind: 'bar', bar: number }) { const isFoo = obj && obj.kind === 'foo'; const isBar = obj && obj.kind === 'bar'; if (isFoo) { obj.foo; } if (isBar) { obj.bar; } } // Narrowing by aliased discriminant property access function f30(obj: { kind: 'foo', foo: string } | { kind: 'bar', bar: number }) { const kind = obj.kind; if (kind === 'foo') { obj.foo; } else { obj.bar; } } function f31(obj: { kind: 'foo', foo: string } | { kind: 'bar', bar: number }) { const { kind } = obj; if (kind === 'foo') { obj.foo; } else { obj.bar; } } function f32(obj: { kind: 'foo', foo: string } | { kind: 'bar', bar: number }) { const { kind: k } = obj; if (k === 'foo') { obj.foo; } else { obj.bar; } } function f33(obj: { kind: 'foo', foo: string } | { kind: 'bar', bar: number }) { const { kind } = obj; switch (kind) { case 'foo': obj.foo; break; case 'bar': obj.bar; break; } } class C10 { constructor(readonly x: string | number) { const thisX_isString = typeof this.x === 'string'; const xIsString = typeof x === 'string'; if (thisX_isString && xIsString) { let s: string; s = this.x; s = x; } } } class C11 { constructor(readonly x: string | number) { const thisX_isString = typeof this.x === 'string'; const xIsString = typeof x === 'string'; if (thisX_isString && xIsString) { // Some narrowings may be invalidated due to later assignments. let s: string; s = this.x; s = x; } else { this.x = 10; x = 10; } } } // Mixing of aliased discriminants and conditionals function f40(obj: { kind: 'foo', foo?: string } | { kind: 'bar', bar?: number }) { const { kind } = obj; const isFoo = kind == 'foo'; if (isFoo && obj.foo) { let t: string = obj.foo; } } // Unsupported narrowing of destructured payload by destructured discriminant type Data = { kind: 'str', payload: string } | { kind: 'num', payload: number }; function gg2(obj: Data) { if (obj.kind === 'str') { let t: string = obj.payload; } else { let t: number = obj.payload; } } function foo({ kind, payload }: Data) { if (kind === 'str') { let t: string = payload; } else { let t: number = payload; } } // Repro from #45830 const obj = { fn: () => true }; if (a) { } const a = obj.fn(); // repro from https://github.com/microsoft/TypeScript/issues/53267 class Utils { static isDefined<T>(value: T): value is NonNullable<T> { return value != null; } } class A53267 { public readonly testNumber: number | undefined; foo() { const isNumber = Utils.isDefined(this.testNumber); if (isNumber) { const x: number = this.testNumber; } } }
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/controlFlowAliasingCatchVariables.ts
TypeScript
// @useUnknownInCatchVariables: true,false try {} catch (e) { const isString = typeof e === 'string'; if (isString) { e.toUpperCase(); // e string } if (typeof e === 'string') { e.toUpperCase(); // e string } } try {} catch (e) { const isString = typeof e === 'string'; e = 1; if (isString) { e.toUpperCase(); // e any/unknown } if (typeof e === 'string') { e.toUpperCase(); // e string } }
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/controlFlowAssignmentExpression.ts
TypeScript
let x: string | boolean | number; let obj: any; x = ""; x = x.length; x; // number x = true; (x = "", obj).foo = (x = x.length); x; // number // https://github.com/microsoft/TypeScript/issues/35484 type D = { done: true, value: 1 } | { done: false, value: 2 }; declare function fn(): D; let o: D; if ((o = fn()).done) { const y: 1 = o.value; }
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/controlFlowAssignmentPatternOrder.ts
TypeScript
// @target: esnext // @noEmit: true // https://github.com/microsoft/TypeScript/pull/41094#issuecomment-716044363 declare function f(): void; { let a: 0 | 1 = 0; let b: 0 | 1 | 9; [{ [(a = 1)]: b } = [9, a] as const] = []; const bb: 0 = b; } { let a: 0 | 1 = 1; let b: 0 | 1 | 9; [{ [a]: b } = [9, a = 0] as const] = []; const bb: 9 = b; } { let a: 0 | 1 = 0; let b: 0 | 1 | 8 | 9; [{ [(a = 1)]: b } = [9, a] as const] = [[9, 8] as const]; const bb: 0 | 8 = b; } { let a: 0 | 1 = 1; let b: 0 | 1 | 8 | 9; [{ [a]: b } = [a = 0, 9] as const] = [[8, 9] as const]; const bb: 0 | 8 = b; } // same as above but on left of a binary expression { let a: 0 | 1 = 0; let b: 0 | 1 | 9; [{ [(a = 1)]: b } = [9, a] as const] = [], f(); const bb: 0 = b; } { let a: 0 | 1 = 1; let b: 0 | 1 | 9; [{ [a]: b } = [9, a = 0] as const] = [], f(); const bb: 9 = b; } { let a: 0 | 1 = 0; let b: 0 | 1 | 8 | 9; [{ [(a = 1)]: b } = [9, a] as const] = [[9, 8] as const], f(); const bb: 0 | 8 = b; } { let a: 0 | 1 = 1; let b: 0 | 1 | 8 | 9; [{ [a]: b } = [a = 0, 9] as const] = [[8, 9] as const], f(); const bb: 0 | 8 = b; } // same as above but on right of a binary expression { let a: 0 | 1 = 0; let b: 0 | 1 | 9; f(), [{ [(a = 1)]: b } = [9, a] as const] = []; const bb: 0 = b; } { let a: 0 | 1 = 1; let b: 0 | 1 | 9; f(), [{ [a]: b } = [9, a = 0] as const] = []; const bb: 9 = b; } { let a: 0 | 1 = 0; let b: 0 | 1 | 8 | 9; f(), [{ [(a = 1)]: b } = [9, a] as const] = [[9, 8] as const]; const bb: 0 | 8 = b; } { let a: 0 | 1 = 1; let b: 0 | 1 | 8 | 9; f(), [{ [a]: b } = [a = 0, 9] as const] = [[8, 9] as const]; const bb: 0 | 8 = b; }
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/controlFlowBinaryAndExpression.ts
TypeScript
let x: string | number | boolean; let cond: boolean; (x = "") && (x = 0); x; // string | number x = ""; cond && (x = 0); x; // string | number
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/controlFlowBinaryOrExpression.ts
TypeScript
let x: string | number | boolean; let cond: boolean; (x = "") || (x = 0); x; // string | number x = ""; cond || (x = 0); x; // string | number export interface NodeList { length: number; } export interface HTMLCollection { length: number; } declare function isNodeList(sourceObj: any): sourceObj is NodeList; declare function isHTMLCollection(sourceObj: any): sourceObj is HTMLCollection; type EventTargetLike = {a: string} | HTMLCollection | NodeList; var sourceObj: EventTargetLike = <any>undefined; if (isNodeList(sourceObj)) { sourceObj.length; } if (isHTMLCollection(sourceObj)) { sourceObj.length; } if (isNodeList(sourceObj) || isHTMLCollection(sourceObj)) { sourceObj.length; }
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/controlFlowBindingElement.ts
TypeScript
// @strictNullChecks: true // @allowUnreachableCode: false { const data = { param: 'value' }; const { param = (() => { throw new Error('param is not defined') })(), } = data; console.log(param); // should not trigger 'Unreachable code detected.' } { const data = { param: 'value' }; let foo: string | undefined = ""; const { param = (() => { throw new Error('param is not defined') })(), } = data; foo; // should be string } { const data = { param: 'value' }; let foo: string | undefined = ""; const { param = (() => { foo = undefined })(), } = data; foo; // should be string | undefined } { const data = { param: 'value' }; let foo: string | undefined = ""; const { param = (() => { return "" + 1 })(), } = data; foo; // should be string } { interface Window { window: Window; } let foo: string | undefined; let window = {} as Window; window.window = window; const { [(() => { foo = ""; return 'window' as const })()]: { [(() => { return 'window' as const })()]: bar } } = window; foo; // should be string } { interface Window { window: Window; } let foo: string | undefined; let window = {} as Window; window.window = window; const { [(() => { return 'window' as const })()]: { [(() => { foo = ""; return 'window' as const })()]: bar } } = window; foo; // should be string } { interface Window { window: Window; } let foo: string | undefined; let window = {} as Window; window.window = window; const { [(() => { return 'window' as const })()]: { [(() => { return 'window' as const })()]: bar = (() => { foo = ""; return window; })() } } = window; foo; // should be string | undefined }
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/controlFlowBindingPatternOrder.ts
TypeScript
// @target: esnext // @noEmit: true // https://github.com/microsoft/TypeScript/pull/41094#issuecomment-716044363 { let a: 0 | 1 = 0; const [{ [(a = 1)]: b } = [9, a] as const] = []; const bb: 0 = b; } { let a: 0 | 1 = 1; const [{ [a]: b } = [9, a = 0] as const] = []; const bb: 9 = b; } { let a: 0 | 1 | 2 = 1; const [{ [a]: b } = [9, a = 0, 5] as const] = []; const bb: 0 | 9 = b; } { let a: 0 | 1 = 0; const [{ [(a = 1)]: b } = [9, a] as const] = [[9, 8] as const]; const bb: 0 | 8 = b; } { let a: 0 | 1 = 1; const [{ [a]: b } = [a = 0, 9] as const] = [[8, 9] as const]; const bb: 0 | 8 = b; }
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/controlFlowCommaOperator.ts
TypeScript
function f(x: string | number | boolean) { let y: string | number | boolean = false; let z: string | number | boolean = false; if (y = "", typeof x === "string") { x; // string y; // string z; // boolean } else if (z = 1, typeof x === "number") { x; // number y; // string z; // number } else { x; // boolean y; // string z; // number } x; // string | number | boolean y; // string z; // number | boolean }
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/controlFlowComputedPropertyNames.ts
TypeScript
// @strict: true // @noEmit: true function f1(obj: Record<string, unknown>, key: string) { if (typeof obj[key] === "string") { obj[key].toUpperCase(); } } function f2(obj: Record<string, string | undefined>, key: string) { if (obj[key] !== undefined) { obj[key].toUpperCase(); } let key2 = key + key; if (obj[key2] !== undefined) { obj[key2].toUpperCase(); } const key3 = key + key; if (obj[key3] !== undefined) { obj[key3].toUpperCase(); } } type Thing = { a?: string, b?: number, c?: number }; function f3(obj: Thing, key: keyof Thing) { if (obj[key] !== undefined) { if (typeof obj[key] === "string") { obj[key].toUpperCase(); } if (typeof obj[key] === "number") { obj[key].toFixed(); } } } function f4<K extends string>(obj: Record<K, string | undefined>, key: K) { if (obj[key]) { obj[key].toUpperCase(); } }
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/controlFlowConditionalExpression.ts
TypeScript
let x: string | number | boolean; let cond: boolean; cond ? x = "" : x = 3; x; // string | number
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/controlFlowDeleteOperator.ts
TypeScript
// @strictNullChecks: true function f() { let x: { a?: number | string, b: number | string } = { b: 1 }; x.a; x.b; x.a = 1; x.b = 1; x.a; x.b; delete x.a; delete x.b; x.a; x.b; x; delete x; // No effect x; }
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/controlFlowDestructuringDeclaration.ts
TypeScript
// @strictNullChecks: true function f1() { let x: string | number = 1; x; let y: string | undefined = ""; y; } function f2() { let [x]: [string | number] = [1]; x; let [y]: [string | undefined] = [""]; y; let [z = ""]: [string | undefined] = [undefined]; z; } function f3() { let [x]: (string | number)[] = [1]; x; let [y]: (string | undefined)[] = [""]; y; let [z = ""]: (string | undefined)[] = [undefined]; z; } function f4() { let { x }: { x: string | number } = { x: 1 }; x; let { y }: { y: string | undefined } = { y: "" }; y; let { z = "" }: { z: string | undefined } = { z: undefined }; z; } function f5() { let { x }: { x?: string | number } = { x: 1 }; x; let { y }: { y?: string | undefined } = { y: "" }; y; let { z = "" }: { z?: string | undefined } = { z: undefined }; z; } function f6() { let { x }: { x?: string | number } = {}; x; let { y }: { y?: string | undefined } = {}; y; let { z = "" }: { z?: string | undefined } = {}; z; } function f7() { let o: { [x: string]: number } = { x: 1 }; let { x }: { [x: string]: string | number } = o; x; }
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/controlFlowDoWhileStatement.ts
TypeScript
let cond: boolean; function a() { let x: string | number; x = ""; do { x; // string } while (cond) } function b() { let x: string | number; x = ""; do { x; // string x = 42; break; } while (cond) } function c() { let x: string | number; x = ""; do { x; // string x = undefined; if (typeof x === "string") continue; break; } while (cond) } function d() { let x: string | number; x = 1000; do { x; // number x = ""; } while (x = x.length) x; // number } function e() { let x: string | number; x = ""; do { x = 42; } while (cond) x; // number } function f() { let x: string | number | boolean | RegExp | Function; x = ""; do { if (cond) { x = 42; break; } if (cond) { x = true; continue; } x = /a/; } while (cond) x; // number | boolean | RegExp } function g() { let x: string | number | boolean | RegExp | Function; x = ""; do { if (cond) { x = 42; break; } if (cond) { x = true; continue; } x = /a/; } while (true) x; // number }
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/controlFlowElementAccess.ts
TypeScript
let x: { o: boolean } = { o: false } if (x['o'] === false) { x['o'] = true } const y: [number, number] = [0, 0]; if (y[0] === 0) { y[0] = -1; }
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/controlFlowElementAccess2.ts
TypeScript
// @strict: true declare const config: { [key: string]: boolean | { prop: string }; }; if (typeof config['works'] !== 'boolean') { config.works.prop = 'test'; // ok config['works'].prop = 'test'; // error, config['works']: boolean | { 'prop': string } } if (typeof config.works !== 'boolean') { config['works'].prop = 'test'; // error, config['works']: boolean | { 'prop': string } config.works.prop = 'test'; // ok }
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/controlFlowElementAccessNoCrash1.ts
TypeScript
// @strict: true // @noEmit: true interface TestTscEdit { caption: string; commandLineArgs?: readonly string[]; } interface TestTscCompile { subScenario: string; commandLineArgs: readonly string[]; } interface VerifyTscEditDiscrepanciesInput { index: number; edits: readonly TestTscEdit[]; commandLineArgs: TestTscCompile["commandLineArgs"]; } function testTscCompile(input: TestTscCompile) {} function verifyTscEditDiscrepancies({ index, edits, commandLineArgs, }: VerifyTscEditDiscrepanciesInput) { const { caption } = edits[index]; testTscCompile({ subScenario: caption, commandLineArgs: edits[index].commandLineArgs || commandLineArgs, }); }
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/controlFlowForInStatement.ts
TypeScript
let x: string | number | boolean | RegExp | Function; let obj: any; let cond: boolean; x = /a/; for (let y in obj) { x = y; if (cond) { x = 42; continue; } if (cond) { x = true; break; } } x; // RegExp | string | number | boolean
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/controlFlowForInStatement2.ts
TypeScript
const keywordA = 'a'; const keywordB = 'b'; type A = { [keywordA]: number }; type B = { [keywordB]: string }; declare const c: A | B; if ('a' in c) { c; // narrowed to `A` } if (keywordA in c) { c; // also narrowed to `A` } let stringB: string = 'b'; if ((stringB as 'b') in c) { c; // narrowed to `B` } if ((stringB as ('a' | 'b')) in c) { c; // not narrowed }
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/controlFlowForOfStatement.ts
TypeScript
let obj: number[]; let x: string | number | boolean | RegExp; function a() { x = true; for (x of obj) { x = x.toExponential(); } x; // string | boolean }
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/controlFlowForStatement.ts
TypeScript
let cond: boolean; function a() { let x: string | number | boolean; for (x = ""; cond; x = 5) { x; // string | number } } function b() { let x: string | number | boolean; for (x = 5; cond; x = x.length) { x; // number x = ""; } } function c() { let x: string | number | boolean; for (x = 5; x = x.toExponential(); x = 5) { x; // string } } function d() { let x: string | number | boolean; for (x = ""; typeof x === "string"; x = 5) { x; // string } } function e() { let x: string | number | boolean | RegExp; for (x = "" || 0; typeof x !== "string"; x = "" || true) { x; // number | boolean } } function f() { let x: string | number | boolean; for (; typeof x !== "string";) { x; // number | boolean if (typeof x === "number") break; x = undefined; } x; // string | number }
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/controlFlowGenericTypes.ts
TypeScript
// @strict: true function f1<T extends string | undefined>(x: T, y: { a: T }, z: [T]): string { if (x) { x; x.length; return x; } if (y.a) { y.a.length; return y.a; } if (z[0]) { z[0].length; return z[0]; } return "hello"; } function f2<T>(x: Extract<T, string | undefined> | null): string { if (x) { x; x.length; return x; } return "hello"; } interface Box<T> { item: T; } declare function isBox(x: any): x is Box<unknown>; declare function isUndefined(x: unknown): x is undefined; declare function unbox<T>(x: Box<T>): T; function g1<T extends Box<T> | undefined>(x: T) { if (isBox(x)) { unbox(x); } } function g2<T extends Box<T> | undefined>(x: T) { if (!isUndefined(x)) { unbox(x); } } function g3<T extends Box<T> | undefined>(x: T) { if (!isBox(x)) { unbox(x); // Error } } function g4<T extends Box<T> | undefined>(x: T) { if (isUndefined(x)) { unbox(x); // Error } } // Repro from #13995 declare function takeA(val: 'A'): void; export function bounceAndTakeIfA<AB extends 'A' | 'B'>(value: AB): AB { if (value === 'A') { takeA(value); return value; } else { return value; } } // Repro from #13995 type Common = { id: number }; type AA = { tag: 'A', id: number }; type BB = { tag: 'B', id: number, foo: number }; type MyUnion = AA | BB; const fn = (value: MyUnion) => { value.foo; // Error if ('foo' in value) { value.foo; } if (value.tag === 'B') { value.foo; } }; const fn2 = <T extends MyUnion>(value: T): MyUnion => { value.foo; // Error if ('foo' in value) { value.foo; } if (value.tag === 'B') { value.foo; } }; // Repro from #13995 type A1 = { testable: true doTest: () => void } type B1 = { testable: false }; type Union = A1 | B1 function notWorking<T extends Union>(object: T) { if (!object.testable) return; object.doTest(); } // Repro from #42939 interface A { a: number | null; }; function get<K extends keyof A>(key: K, obj: A): number { const value = obj[key]; if (value !== null) { return value; } return 0; }; // Repro from #44093 class EventEmitter<ET> { off<K extends keyof ET>(...args: [K, number] | [unknown, string]):void {} } function once<ET, T extends EventEmitter<ET>>(emittingObject: T, eventName: keyof ET): void { emittingObject.off(eventName, 0); emittingObject.off(eventName as typeof eventName, 0); } // In an element access obj[x], we consider obj to be in a constraint position, except when obj is of // a generic type without a nullable constraint and x is a generic type. This is because when both obj // and x are of generic types T and K, we want the resulting type to be T[K]. function fx1<T, K extends keyof T>(obj: T, key: K) { const x1 = obj[key]; const x2 = obj && obj[key]; } function fx2<T extends Record<keyof T, string>, K extends keyof T>(obj: T, key: K) { const x1 = obj[key]; const x2 = obj && obj[key]; } function fx3<T extends Record<keyof T, string> | undefined, K extends keyof T>(obj: T, key: K) { const x1 = obj[key]; // Error const x2 = obj && obj[key]; } // Repro from #44166 class TableBaseEnum< PublicSpec extends Record<keyof InternalSpec, any>, InternalSpec extends Record<keyof PublicSpec, any> | undefined = undefined> { m() { let iSpec = null! as InternalSpec; iSpec[null! as keyof InternalSpec]; // Error, object possibly undefined iSpec[null! as keyof PublicSpec]; // Error, object possibly undefined if (iSpec === undefined) { return; } iSpec[null! as keyof InternalSpec]; iSpec[null! as keyof PublicSpec]; } } // Repros from #45145 function f10<T extends { a: string } | undefined>(x: T, y: Partial<T>) { y = x; } type SqlInsertSet<T> = T extends undefined ? object : { [P in keyof T]: unknown }; class SqlTable<T> { protected validateRow(_row: Partial<SqlInsertSet<T>>): void { } public insertRow(row: SqlInsertSet<T>) { this.validateRow(row); } } // Repro from #46495 interface Button { type: "button"; text: string; } interface Checkbox { type: "checkbox"; isChecked: boolean; } type Control = Button | Checkbox; function update<T extends Control, K extends keyof T>(control : T | undefined, key: K, value: T[K]): void { if (control !== undefined) { control[key] = value; } } // Repro from #50465 type Column<T> = (keyof T extends never ? { id?: number | string } : { id: T }) & { title?: string; } function getColumnProperty<T>(column: Column<T>, key: keyof Column<T>) { return column[key]; }
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/controlFlowIIFE.ts
TypeScript
// @strictNullChecks: true // @target: ES2017 declare function getStringOrNumber(): string | number; function f1() { let x = getStringOrNumber(); if (typeof x === "string") { let n = function() { return x.length; }(); } } function f2() { let x = getStringOrNumber(); if (typeof x === "string") { let n = (function() { return x.length; })(); } } function f3() { let x = getStringOrNumber(); let y: number; if (typeof x === "string") { let n = (z => x.length + y + z)(y = 1); } } // Repros from #8381 let maybeNumber: number | undefined; (function () { maybeNumber = 1; })(); maybeNumber++; if (maybeNumber !== undefined) { maybeNumber++; } let test: string | undefined; if (!test) { throw new Error('Test is not defined'); } (() => { test.slice(1); // No error })(); // Repro from #23565 function f4() { let v: number; (function() { v = 1; })(); v; } function f5() { let v: number; (function*() { yield 1; v = 1; })(); v; // still undefined } function f6() { let v: number; (async function() { v = await 1; })(); v; // still undefined }
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/controlFlowIfStatement.ts
TypeScript
// @allowUnreachableCode: true let x: string | number | boolean | RegExp; let cond: boolean; x = /a/; if (x /* RegExp */, (x = true)) { x; // boolean x = ""; } else { x; // boolean x = 42; } x; // string | number function a() { let x: string | number; if (cond) { x = 42; } else { x = ""; return; } x; // number } function b() { let x: string | number; if (cond) { x = 42; throw ""; } else { x = ""; } x; // string } function c<T>(data: string | T): T { if (typeof data === 'string') { return JSON.parse(data); } else { return data; } } function d<T extends string>(data: string | T): never { if (typeof data === 'string') { throw new Error('will always happen'); } else { return data; } } interface I<T> { p: T; } function e(x: I<"A" | "B">) { if (x.p === "A") { let a: "A" = (null as unknown as typeof x.p) } }
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/controlFlowInOperator.ts
TypeScript
const a = 'a'; const b = 'b'; const d = 'd'; type A = { [a]: number; }; type B = { [b]: string; }; declare const c: A | B; if ('a' in c) { c; // A c['a']; // number; } if ('d' in c) { c; // never } if (a in c) { c; // A c[a]; // number; } if (d in c) { c; // never } // repro from https://github.com/microsoft/TypeScript/issues/54790 function uniqueID_54790( id: string | undefined, seenIDs: { [key: string]: string } ): string { if (id === undefined) { id = "1"; } if (!(id in seenIDs)) { return id; } for (let i = 1; i < Number.MAX_VALUE; i++) { const newID = `${id}-${i}`; if (!(newID in seenIDs)) { return newID; } } throw Error("heat death of the universe"); } function uniqueID_54790_2(id: string | number, seenIDs: object) { id = "a"; for (let i = 1; i < 3; i++) { const newID = `${id}`; if (newID in seenIDs) { } } } function uniqueID_54790_3(id: string | number, seenIDs: object) { id = "a"; for (let i = 1; i < 3; i++) { const newID = id; if (newID in seenIDs) { } } }
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/controlFlowInstanceOfGuardPrimitives.ts
TypeScript
function distinguish(thing: string | number | Date) { if (thing instanceof Object) { console.log("Aha!! It's a Date in " + thing.getFullYear()); } else if (typeof thing === 'string') { console.log("Aha!! It's a string of length " + thing.length); } else { console.log("Aha!! It's the number " + thing.toPrecision(3)); } } distinguish(new Date()); distinguish("beef"); distinguish(3.14159265);
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/controlFlowInstanceofExtendsFunction.ts
TypeScript
declare global { interface Function { now(): string; } } Function.prototype.now = function () { return "now" } class X { static now() { return {} } why() { } } class Y { } console.log(X.now()) // works as expected console.log(Y.now()) // works as expected export const x: X | number = Math.random() > 0.5 ? new X() : 1 if (x instanceof X) { x.why() // should compile }
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/controlFlowIteration.ts
TypeScript
// @strictNullChecks: true let cond: boolean; function ff() { let x: string | undefined; while (true) { if (cond) { x = ""; } else { if (x) { x.length; } if (x) { x.length; } } } }
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/controlFlowIterationErrors.ts
TypeScript
// @noImplicitAny: true let cond: boolean; function len(s: string) { return s.length; } function f1() { let x: string | number | boolean; x = ""; while (cond) { x = len(x); x; } x; } function f2() { let x: string | number | boolean; x = ""; while (cond) { x; x = len(x); } x; } declare function foo(x: string): number; declare function foo(x: number): string; function g1() { let x: string | number | boolean; x = ""; while (cond) { x = foo(x); x; } x; } function g2() { let x: string | number | boolean; x = ""; while (cond) { x; x = foo(x); } x; } function asNumber(x: string | number): number { return +x; } function h1() { let x: string | number | boolean; x = "0"; while (cond) { x = +x + 1; x; } } function h2() { let x: string | number | boolean; x = "0"; while (cond) { x = asNumber(x) + 1; x; } } function h3() { let x: string | number | boolean; x = "0"; while (cond) { let y = asNumber(x); x = y + 1; x; } } function h4() { let x: string | number | boolean; x = "0"; while (cond) { x; let y = asNumber(x); x = y + 1; x; } }
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/controlFlowIterationErrorsAsync.ts
TypeScript
// @strict: true // @noEmit: true // @lib: esnext let cond: boolean; async function len(s: string) { return s.length; } async function f1() { let x: string | number | boolean; x = ""; while (cond) { x = await len(x); x; } x; } async function f2() { let x: string | number | boolean; x = ""; while (cond) { x; x = await len(x); } x; } declare function foo(x: string): Promise<number>; declare function foo(x: number): Promise<string>; async function g1() { let x: string | number | boolean; x = ""; while (cond) { x = await foo(x); x; } x; } async function g2() { let x: string | number | boolean; x = ""; while (cond) { x; x = await foo(x); } x; } async function asNumber(x: string | number): Promise<number> { return +x; } async function h1() { let x: string | number | boolean; x = "0"; while (cond) { x = +x + 1; x; } } async function h2() { let x: string | number | boolean; x = "0"; while (cond) { x = await asNumber(x) + 1; x; } } async function h3() { let x: string | number | boolean; x = "0"; while (cond) { let y = await asNumber(x); x = y + 1; x; } } async function h4() { let x: string | number | boolean; x = "0"; while (cond) { x; let y = await asNumber(x); x = y + 1; x; } } // repro #51115 async function get_things(_: number | undefined) { return [0]; } async function foobar() { let before: number | undefined = undefined; for (let i = 0; i < 2; i++) { const results = await get_things(before); before = results[0]; } } // repro #43047#issuecomment-821453073 declare function foox(x: string | undefined): Promise<string> async () => { let bar: string | undefined = undefined; do { const baz = await foox(bar); bar = baz } while (bar) } // repro #43047#issuecomment-874221939 declare function myQuery(input: { lastId: number | undefined }): Promise<{ entities: number[] }>; async function myFunc(): Promise<void> { let lastId: number | undefined = undefined; while (true) { const { entities } = await myQuery({ lastId, }); lastId = entities[entities.length - 1]; } }
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/controlFlowNoIntermediateErrors.ts
TypeScript
// @strict: true // @noEmit: true // Repros from #46475 function f1() { let code: 0 | 1 | 2 = 0; const otherCodes: (0 | 1 | 2)[] = [2, 0, 1, 0, 2, 2, 2, 0, 1, 0, 2, 1, 1, 0, 2, 1]; for (const code2 of otherCodes) { if (code2 === 0) { code = code === 2 ? 1 : 0; } else { code = 2; } } } function f2() { let code: 0 | 1 = 0; while (true) { code = code === 1 ? 0 : 1; } }
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/controlFlowNullishCoalesce.ts
TypeScript
// @strict: true // assignments in shortcutting rhs let a: number; o ?? (a = 1); a.toString(); // assignment flow declare const o: { x: number } | undefined; let x: { x: number } | boolean; if (x = o ?? true) { x; }
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/controlFlowOptionalChain.ts
TypeScript
// @strict: true // @allowUnreachableCode: false // assignments in shortcutting chain declare const o: undefined | { [key: string]: any; [key: number]: any; (...args: any[]): any; }; let a: number; o?.[a = 1]; a.toString(); let b: number; o?.x[b = 1]; b.toString(); let c: number; o?.(c = 1) c.toString(); let d: number; o?.x(d = 1); d.toString(); // type predicates declare const f: undefined | ((x: any) => x is number); declare const x: string | number; if (f?.(x)) { x; // number f; // (x: any) => x is number f(x); } else { x; f; f(x); } x; f; f(x); declare const o2: { f(x: any): x is number; } | undefined; if (o2?.f(x)) { x; // number o2.f; // (x: any) => x is number o2?.f; o2?.f(x); } else { x; o2; o2?.f; o2.f; } x; o2; o2?.f; o2.f; declare const o3: { x: 1, y: string } | { x: 2, y: number } | undefined; if (o3?.x === 1) { o3; o3.x; o3?.x; } else { o3; o3?.x; o3.x; } o3; o3?.x; o3.x; declare const o4: { x?: { y: boolean } }; if (o4.x?.y) { o4.x; // { y: boolean } o4.x.y; // true o4.x?.y; // true } else { o4.x; o4.x?.y; o4.x.y; } o4.x; o4.x?.y; o4.x.y; declare const o5: { x?: { y: { z?: { w: boolean } } } }; if (o5.x?.y.z?.w) { o5.x; o5.x.y; o5.x.y.z; o5.x.y.z.w; // true o5.x.y.z?.w; // true o5.x?.y.z.w; // true o5.x?.y.z?.w; // true } else { o5.x; o5.x?.y; o5.x?.y.z; o5.x?.y.z?.w; o5.x.y; o5.x.y.z.w; } o5.x; o5.x?.y; o5.x?.y.z; o5.x?.y.z?.w; o5.x.y; o5.x.y.z.w; interface Base { f(): this is Derived; } interface Derived extends Base { x: number; } declare const o6: Base | undefined; if (o6?.f()) { o6; // Derived o6.f; } else { o6; o6?.f; o6.f; } o6; o6?.f; o6.f; // asserts declare const isDefined: <T>(value: T) => asserts value is NonNullable<T>; declare const isString: (value: unknown) => asserts value is string; declare const maybeIsString: undefined | ((value: unknown) => asserts value is string); declare const maybeNever: undefined | (() => never); function f01(x: unknown) { if (!!true) { isString?.(x); x; } if (!!true) { maybeIsString?.(x); x; } if (!!true) { isDefined(maybeIsString); maybeIsString?.(x); x; } if (!!true) { maybeNever?.(); x; } } type Thing = { foo: string | number, bar(): number, baz: object }; function f10(o: Thing | undefined, value: number) { if (o?.foo === value) { o.foo; } if (o?.["foo"] === value) { o["foo"]; } if (o?.bar() === value) { o.bar; } if (o?.foo == value) { o.foo; } if (o?.["foo"] == value) { o["foo"]; } if (o?.bar() == value) { o.bar; } } function f11(o: Thing | null, value: number) { if (o?.foo === value) { o.foo; } if (o?.["foo"] === value) { o["foo"]; } if (o?.bar() === value) { o.bar; } if (o?.foo == value) { o.foo; } if (o?.["foo"] == value) { o["foo"]; } if (o?.bar() == value) { o.bar; } } function f12(o: Thing | undefined, value: number | undefined) { if (o?.foo === value) { o.foo; // Error } if (o?.["foo"] === value) { o["foo"]; // Error } if (o?.bar() === value) { o.bar; // Error } if (o?.foo == value) { o.foo; // Error } if (o?.["foo"] == value) { o["foo"]; // Error } if (o?.bar() == value) { o.bar; // Error } } function f12a(o: Thing | undefined, value: number | null) { if (o?.foo === value) { o.foo; } if (o?.["foo"] === value) { o["foo"]; } if (o?.bar() === value) { o.bar; } if (o?.foo == value) { o.foo; // Error } if (o?.["foo"] == value) { o["foo"]; // Error } if (o?.bar() == value) { o.bar; // Error } } function f13(o: Thing | undefined) { if (o?.foo !== undefined) { o.foo; } if (o?.["foo"] !== undefined) { o["foo"]; } if (o?.bar() !== undefined) { o.bar; } if (o?.foo != undefined) { o.foo; } if (o?.["foo"] != undefined) { o["foo"]; } if (o?.bar() != undefined) { o.bar; } } function f13a(o: Thing | undefined) { if (o?.foo !== null) { o.foo; // Error } if (o?.["foo"] !== null) { o["foo"]; // Error } if (o?.bar() !== null) { o.bar; // Error } if (o?.foo != null) { o.foo; } if (o?.["foo"] != null) { o["foo"]; } if (o?.bar() != null) { o.bar; } } function f14(o: Thing | null) { if (o?.foo !== undefined) { o.foo; } if (o?.["foo"] !== undefined) { o["foo"]; } if (o?.bar() !== undefined) { o.bar; } } function f15(o: Thing | undefined, value: number) { if (o?.foo === value) { o.foo; } else { o.foo; // Error } if (o?.foo !== value) { o.foo; // Error } else { o.foo; } if (o?.foo == value) { o.foo; } else { o.foo; // Error } if (o?.foo != value) { o.foo; // Error } else { o.foo; } } function f15a(o: Thing | undefined, value: unknown) { if (o?.foo === value) { o.foo; // Error } else { o.foo; // Error } if (o?.foo !== value) { o.foo; // Error } else { o.foo; // Error } if (o?.foo == value) { o.foo; // Error } else { o.foo; // Error } if (o?.foo != value) { o.foo; // Error } else { o.foo; // Error } } function f16(o: Thing | undefined) { if (o?.foo === undefined) { o.foo; // Error } else { o.foo; } if (o?.foo !== undefined) { o.foo; } else { o.foo; // Error } if (o?.foo == undefined) { o.foo; // Error } else { o.foo; } if (o?.foo != undefined) { o.foo; } else { o.foo; // Error } } function f20(o: Thing | undefined) { if (typeof o?.foo === "number") { o.foo; } if (typeof o?.["foo"] === "number") { o["foo"]; } if (typeof o?.bar() === "number") { o.bar; } if (o?.baz instanceof Error) { o.baz; } } function f21(o: Thing | null) { if (typeof o?.foo === "number") { o.foo; } if (typeof o?.["foo"] === "number") { o["foo"]; } if (typeof o?.bar() === "number") { o.bar; } if (o?.baz instanceof Error) { o.baz; } } function f22(o: Thing | undefined) { if (typeof o?.foo === "number") { o.foo; } else { o.foo; // Error } if (typeof o?.foo !== "number") { o.foo; // Error } else { o.foo; } if (typeof o?.foo == "number") { o.foo; } else { o.foo; // Error } if (typeof o?.foo != "number") { o.foo; // Error } else { o.foo; } } function f23(o: Thing | undefined) { if (typeof o?.foo === "undefined") { o.foo; // Error } else { o.foo; } if (typeof o?.foo !== "undefined") { o.foo; } else { o.foo; // Error } if (typeof o?.foo == "undefined") { o.foo; // Error } else { o.foo; } if (typeof o?.foo != "undefined") { o.foo; } else { o.foo; // Error } } declare function assert(x: unknown): asserts x; declare function assertNonNull<T>(x: T): asserts x is NonNullable<T>; function f30(o: Thing | undefined) { if (!!true) { assert(o?.foo); o.foo; } if (!!true) { assert(o?.foo === 42); o.foo; } if (!!true) { assert(typeof o?.foo === "number"); o.foo; } if (!!true) { assertNonNull(o?.foo); o.foo; } } function f40(o: Thing | undefined) { switch (o?.foo) { case "abc": o.foo; break; case 42: o.foo; break; case undefined: o.foo; // Error break; default: o.foo; // Error break; } } function f41(o: Thing | undefined) { switch (typeof o?.foo) { case "string": o.foo; break; case "number": o.foo; break; case "undefined": o.foo; // Error break; default: o.foo; // Error break; } } // Repros from #34570 type Shape = | { type: 'rectangle', width: number, height: number } | { type: 'circle', radius: number } function getArea(shape?: Shape) { switch (shape?.type) { case 'circle': return Math.PI * shape.radius ** 2 case 'rectangle': return shape.width * shape.height default: return 0 } } type Feature = { id: string; geometry?: { type: string; coordinates: number[]; }; }; function extractCoordinates(f: Feature): number[] { if (f.geometry?.type !== 'test') { return []; } return f.geometry.coordinates; } // Repro from #35842 interface SomeObject { someProperty: unknown; } let lastSomeProperty: unknown | undefined; function someFunction(someOptionalObject: SomeObject | undefined): void { if (someOptionalObject?.someProperty !== lastSomeProperty) { console.log(someOptionalObject); console.log(someOptionalObject.someProperty); // Error lastSomeProperty = someOptionalObject?.someProperty; } } const someObject: SomeObject = { someProperty: 42 }; someFunction(someObject); someFunction(undefined); // Repro from #35970 let i = 0; declare const arr: { tag: ("left" | "right") }[]; while (arr[i]?.tag === "left") { i += 1; if (arr[i]?.tag === "right") { console.log("I should ALSO be reachable"); } } // Repro from #51941 type Test5 = { main?: { childs: Record<string, Test5>; }; }; function f50(obj: Test5) { for (const key in obj.main?.childs) { if (obj.main.childs[key] === obj) { return obj; } } return null; }
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/controlFlowOptionalChain2.ts
TypeScript
// @strictNullChecks: true type A = { type: 'A'; name: string; } type B = { type: 'B'; } function funcTwo(arg: A | B | undefined) { if (arg?.type === 'B') { arg; // `B` return; } arg; arg?.name; } function funcThree(arg: A | B | null) { if (arg?.type === 'B') { arg; // `B` return; } arg; arg?.name; } type U = { kind: undefined, u: 'u' } type N = { kind: null, n: 'n' } type X = { kind: 'X', x: 'x' } function f1(x: X | U | undefined) { if (x?.kind === undefined) { x; // U | undefined } else { x; // X } } function f2(x: X | N | undefined) { if (x?.kind === undefined) { x; // undefined } else { x; // X | N } } function f3(x: X | U | null) { if (x?.kind === undefined) { x; // U | null } else { x; // X } } function f4(x: X | N | null) { if (x?.kind === undefined) { x; // null } else { x; // X | N } } function f5(x: X | U | undefined) { if (x?.kind === null) { x; // never } else { x; // X | U | undefined } } function f6(x: X | N | undefined) { if (x?.kind === null) { x; // N } else { x; // X | undefined } } function f7(x: X | U | null) { if (x?.kind === null) { x; // never } else { x; // X | U | null } } function f8(x: X | N | null) { if (x?.kind === null) { x; // N } else { x; // X | null } }
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/controlFlowOptionalChain3.tsx
TypeScript (TSX)
// @strict: true // @noEmit: true // @esModuleInterop: true // @jsx: react /// <reference path="/.lib/react16.d.ts" /> // https://github.com/microsoft/TypeScript/issues/56482 import React from "react"; interface Foo { bar: boolean; } function test1(foo: Foo | undefined) { if (foo?.bar === false) { foo; } foo; } function test2(foo: Foo | undefined) { if (foo?.bar === false) { foo; } else { foo; } } function Test3({ foo }: { foo: Foo | undefined }) { return ( <div> {foo?.bar === false && "foo"} {foo.bar ? "true" : "false"} </div> ); } function test4(options?: { a?: boolean; b?: boolean }) { if (options?.a === false || options.b) { options; } }
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/controlFlowParameter.ts
TypeScript
// @strictNullChecks: true // @allowUnreachableCode: false function f1( required: unknown = (() => { throw new Error("bad"); })() ) { console.log("ok"); // should not trigger 'Unreachable code detected.' } function f2( a: number | string | undefined, required: unknown = (() => { a = 1; })() ) { a; // should be number | string | undefined } function f3( a: number | string | undefined = 1, required: unknown = (() => { a = ""; })() ) { a; // should be number | string } function f4( a: number | string | undefined = 1, { [(a = "")]: b } = {} as any ) { a; // should be string }
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/controlFlowStringIndex.ts
TypeScript
// @strict: true type A = { other: number | null; [index: string]: number | null }; declare const value: A; if (value.foo !== null) { value.foo.toExponential() value.other // should still be number | null value.bar // should still be number | null }
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/controlFlowSuperPropertyAccess.ts
TypeScript
// @strictNullChecks: true class B { protected m?(): void; } class C extends B { body() { super.m && super.m(); } }
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/controlFlowTruthiness.ts
TypeScript
// @strictNullChecks: true declare function foo(): string | undefined; function f1() { let x = foo(); if (x) { x; // string } else { x; // string | undefined } } function f2() { let x: string | undefined; x = foo(); if (x) { x; // string } else { x; // string | undefined } } function f3() { let x: string | undefined; if (x = foo()) { x; // string } else { x; // string | undefined } } function f4() { let x: string | undefined; if (!(x = foo())) { x; // string | undefined } else { x; // string } } function f5() { let x: string | undefined; let y: string | undefined; if (x = y = foo()) { x; // string y; // string | undefined } else { x; // string | undefined y; // string | undefined } } function f6() { let x: string | undefined; let y: string | undefined; if (x = foo(), y = foo()) { x; // string | undefined y; // string } else { x; // string | undefined y; // string | undefined } } function f7(x: {}) { if (x) { x; // {} } else { x; // {} } } function f8<T>(x: T) { if (x) { x; // {} } else { x; // {} } } function f9<T extends object>(x: T) { if (x) { x; // {} } else { x; // never } }
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/controlFlowTypeofObject.ts
TypeScript
// @strict: true // @declaration: true declare function obj(x: object): void; function f1(x: unknown) { if (!x) { return; } if (typeof x === 'object') { obj(x); } } function f2(x: unknown) { if (x === null) { return; } if (typeof x === 'object') { obj(x); } } function f3(x: unknown) { if (x == null) { return; } if (typeof x === 'object') { obj(x); } } function f4(x: unknown) { if (x == undefined) { return; } if (typeof x === 'object') { obj(x); } } function f5(x: unknown) { if (!!true) { if (!x) { return; } } else { if (x === null) { return; } } if (typeof x === 'object') { obj(x); } } function f6(x: unknown) { if (x === null) { x; } else { x; if (typeof x === 'object') { obj(x); } } if (typeof x === 'object') { obj(x); // Error } }
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/controlFlowWhileStatement.ts
TypeScript
let cond: boolean; function a() { let x: string | number; x = ""; while (cond) { x; // string } } function b() { let x: string | number; x = ""; while (cond) { x; // string x = 42; break; } } function c() { let x: string | number; x = ""; while (cond) { x; // string x = undefined; if (typeof x === "string") continue; break; } } function d() { let x: string | number; x = ""; while (x = x.length) { x; // number x = ""; } } function e() { let x: string | number; x = ""; while (cond) { x; // string | number x = 42; x; // number } x; // string | number } function f() { let x: string | number | boolean | RegExp | Function; x = ""; while (cond) { if (cond) { x = 42; break; } if (cond) { x = true; continue; } x = /a/; } x; // string | number | boolean | RegExp } function g() { let x: string | number | boolean | RegExp | Function; x = ""; while (true) { if (cond) { x = 42; break; } if (cond) { x = true; continue; } x = /a/; } x; // number } function h1() { let x: string | number | boolean; x = ""; while (x > 1) { x; // string | number x = 1; x; // number } x; // string | number } declare function len(s: string | number): number; function h2() { let x: string | number | boolean; x = ""; while (cond) { x = len(x); x; // number } x; // string | number } function h3() { let x: string | number | boolean; x = ""; while (cond) { x; // string | number x = len(x); } x; // string | number }
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/controlFlowWithTemplateLiterals.ts
TypeScript
// @strictNullChecks: true declare const envVar: string | undefined; if (typeof envVar === `string`) { envVar.slice(0) } declare const obj: {test: string} | {} if (`test` in obj) { obj.test.slice(0) }
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/correctlyMarkAliasAsReferences1.tsx
TypeScript (TSX)
// @target: es2017 // @jsx: react // @moduleResolution: node // @skipLibCheck: true // @libFiles: react.d.ts,lib.d.ts // @filename: declaration.d.ts declare module "classnames"; // @filename: 0.tsx ///<reference path="declaration.d.ts" /> import * as cx from 'classnames'; import * as React from "react"; let buttonProps; // any let k = <button {...buttonProps}> <span className={cx('class1', { class2: true })} /> </button>;
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/correctlyMarkAliasAsReferences2.tsx
TypeScript (TSX)
// @target: es2017 // @jsx: react // @moduleResolution: node // @skipLibCheck: true // @libFiles: react.d.ts,lib.d.ts // @filename: declaration.d.ts declare module "classnames"; // @filename: 0.tsx ///<reference path="declaration.d.ts" /> import * as cx from 'classnames'; import * as React from "react"; let buttonProps : {[attributeName: string]: ''} let k = <button {...buttonProps}> <span className={cx('class1', { class2: true })} /> </button>;
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/correctlyMarkAliasAsReferences3.tsx
TypeScript (TSX)
// @target: es2017 // @jsx: react // @moduleResolution: node // @noImplicitAny: true // @skipLibCheck: true // @libFiles: react.d.ts,lib.d.ts // @filename: declaration.d.ts declare module "classnames"; // @filename: 0.tsx ///<reference path="declaration.d.ts" /> import * as cx from 'classnames'; import * as React from "react"; let buttonProps; let k = <button {...buttonProps}> <span className={cx('class1', { class2: true })} /> </button>;
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/correctlyMarkAliasAsReferences4.tsx
TypeScript (TSX)
// @target: es2017 // @jsx: react // @moduleResolution: node // @skipLibCheck: true // @libFiles: react.d.ts,lib.d.ts // @filename: declaration.d.ts declare module "classnames"; // @filename: 0.tsx ///<reference path="declaration.d.ts" /> import * as cx from 'classnames'; import * as React from "react"; let buttonProps : {[attributeName: string]: ''} let k = <button {...buttonProps} className={cx('class1', { class2: true })} />;
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/covariantCallbacks.ts
TypeScript
// @target: es2015 // @strict: true // Test that callback parameters are related covariantly interface P<T> { then(cb: (value: T) => void): void; }; interface A { a: string } interface B extends A { b: string } function f1(a: P<A>, b: P<B>) { a = b; b = a; // Error } function f2(a: Promise<A>, b: Promise<B>) { a = b; b = a; // Error } interface AList1 { forEach(cb: (item: A) => void): void; } interface BList1 { forEach(cb: (item: B) => void): void; } function f11(a: AList1, b: BList1) { a = b; b = a; // Error } interface AList2 { forEach(cb: (item: A) => boolean): void; } interface BList2 { forEach(cb: (item: A) => void): void; } function f12(a: AList2, b: BList2) { a = b; b = a; // Error } interface AList3 { forEach(cb: (item: A) => void): void; } interface BList3 { forEach(cb: (item: A, context: any) => void): void; } function f13(a: AList3, b: BList3) { a = b; b = a; // Error } interface AList4 { forEach(cb: (item: A) => A): void; } interface BList4 { forEach(cb: (item: B) => B): void; } function f14(a: AList4, b: BList4) { a = b; b = a; // Error } // Repro from #51620 type Bivar<T> = { set(value: T): void } declare let bu: Bivar<unknown>; declare let bs: Bivar<string>; bu = bs; bs = bu; declare let bfu: Bivar<(x: unknown) => void>; declare let bfs: Bivar<(x: string) => void>; bfu = bfs; bfs = bfu; type Bivar1<T> = { set(value: T): void } type Bivar2<T> = { set(value: T): void } declare let b1fu: Bivar1<(x: unknown) => void>; declare let b2fs: Bivar2<(x: string) => void>; b1fu = b2fs; b2fs = b1fu; type SetLike<T> = { set(value: T): void, get(): T } declare let sx: SetLike1<(x: unknown) => void>; declare let sy: SetLike1<(x: string) => void>; sx = sy; // Error sy = sx; type SetLike1<T> = { set(value: T): void, get(): T } type SetLike2<T> = { set(value: T): void, get(): T } declare let s1: SetLike1<(x: unknown) => void>; declare let s2: SetLike2<(x: string) => void>; s1 = s2; // Error s2 = s1;
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/declarationEmitIdentifierPredicates01.ts
TypeScript
// @declaration: true // @module: commonjs export function f(x: any): x is number { return typeof x === "number"; }
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/declarationEmitIdentifierPredicatesWithPrivateName01.ts
TypeScript
// @declaration: true // @module: commonjs interface I { a: number; } export function f(x: any): x is I { return typeof x.a === "number"; }
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/declarationEmitReadonly.ts
TypeScript
// @declaration: true class C { constructor(readonly x: number) {} }
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/declarationEmitThisPredicates01.ts
TypeScript
// @declaration: true // @module: commonjs export class C { m(): this is D { return this instanceof D; } } export class D extends C { }
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/declarationEmitThisPredicates02.ts
TypeScript
// @declaration: true // @module: commonjs export interface Foo { a: string; b: number; c: boolean; } export const obj = { m(): this is Foo { let dis = this as {} as Foo; return dis.a != null && dis.b != null && dis.c != null; } }
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/declarationEmitThisPredicatesWithPrivateName01.ts
TypeScript
// @declaration: true // @module: commonjs export class C { m(): this is D { return this instanceof D; } } class D extends C { }
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/declarationEmitThisPredicatesWithPrivateName02.ts
TypeScript
// @declaration: true // @module: commonjs interface Foo { a: string; b: number; c: boolean; } export const obj = { m(): this is Foo { let dis = this as {} as Foo; return dis.a != null && dis.b != null && dis.c != null; } }
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/declarationEmitWorkWithInlineComments.ts
TypeScript
// @declaration: true // @stripInternal:true export class Foo { constructor( /** @internal */ public isInternal1: string, /** @internal */ public isInternal2: string, /** @internal */ public isInternal3: string, // @internal public isInternal4: string, // nothing /** @internal */ public isInternal5: string, /* @internal */ public isInternal6: string /* trailing */, /* @internal */ public isInternal7: string, /** @internal */ // not work public notInternal1: string, // @internal /* not work */ public notInternal2: string, /* not work */ // @internal /* not work */ public notInternal3: string, ) { } } export class Bar { constructor(/* @internal */ public isInternal1: string) {} } export class Baz { constructor(/* @internal */ public isInternal: string ) {} }
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/declarationFileForJsonImport.ts
TypeScript
// @allowArbitraryExtensions: true // @resolveJsonModule: true,false // @filename: main.ts import data from "./data.json"; let x: string = data; // @filename: data.json {} // @filename: data.d.json.ts declare var val: string; export default val;
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/declarationFiles.ts
TypeScript
// @declaration: true class C1 { x: this; f(x: this): this { return undefined; } constructor(x: this) { } } class C2 { [x: string]: this; } interface Foo<T> { x: T; y: this; } class C3 { a: this[]; b: [this, this]; c: this | Date; d: this & Date; e: (((this))); f: (x: this) => this; g: new (x: this) => this; h: Foo<this>; i: Foo<this | (() => this)>; j: (x: any) => x is this; } class C4 { x1 = { a: this }; x2 = [this]; x3 = [{ a: this }]; x4 = () => this; f1() { return { a: this }; } f2() { return [this]; } f3() { return [{ a: this }]; } f4() { return () => this; } }
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/declarationInAmbientContext.ts
TypeScript
declare var [a, b]; // Error, destructuring declaration not allowed in ambient context declare var {c, d}; // Error, destructuring declaration not allowed in ambient context
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/declarationsAndAssignments.ts
TypeScript
function f0() { var [] = [1, "hello"]; var [x] = [1, "hello"]; var [x, y] = [1, "hello"]; var [x, y, z] = [1, "hello"]; var [,, x] = [0, 1, 2]; var x: number; var y: string; } function f1() { var a = [1, "hello"]; var [x] = a; var [x, y] = a; var [x, y, z] = a; var x: number | string; var y: number | string; var z: number | string; } function f2() { var { } = { x: 5, y: "hello" }; // Ok, empty binding pattern means nothing var { x } = { x: 5, y: "hello" }; // Error, no y in target var { y } = { x: 5, y: "hello" }; // Error, no x in target var { x, y } = { x: 5, y: "hello" }; var x: number; var y: string; var { x: a } = { x: 5, y: "hello" }; // Error, no y in target var { y: b } = { x: 5, y: "hello" }; // Error, no x in target var { x: a, y: b } = { x: 5, y: "hello" }; var a: number; var b: string; } function f3() { var [x, [y, [z]]] = [1, ["hello", [true]]]; var x: number; var y: string; var z: boolean; } function f4() { var { a: x, b: { a: y, b: { a: z }}} = { a: 1, b: { a: "hello", b: { a: true } } }; var x: number; var y: string; var z: boolean; } function f6() { var [x = 0, y = ""] = [1, "hello"]; var x: number; var y: string; } function f7() { var [x = 0, y = 1] = [1, "hello"]; // Error, initializer for y must be string var x: number; var y: string; } function f8() { var [a, b, c] = []; // Error, [] is an empty tuple var [d, e, f] = [1]; // Error, [1] is a tuple } function f9() { var [a, b] = {}; // Error, not array type var [c, d] = { 0: 10, 1: 20 }; // Error, not array type var [e, f] = [10, 20]; } function f10() { var { a, b } = {}; // Error var { a, b } = []; // Error } function f11() { var { x: a, y: b } = { x: 10, y: "hello" }; var { 0: a, 1: b } = { 0: 10, 1: "hello" }; var { "<": a, ">": b } = { "<": 10, ">": "hello" }; var { 0: a, 1: b } = [10, "hello"]; var a: number; var b: string; } function f12() { var [a, [b, { x, y: c }] = ["abc", { x: 10, y: false }]] = [1, ["hello", { x: 5, y: true }]]; var a: number; var b: string; var x: number; var c: boolean; } function f13() { var [x, y] = [1, "hello"]; var [a, b] = [[x, y], { x: x, y: y }]; } function f14([a = 1, [b = "hello", { x, y: c = false }]]) { var a: number; var b: string; var c: boolean; } f14([2, ["abc", { x: 0, y: true }]]); f14([2, ["abc", { x: 0 }]]); f14([2, ["abc", { y: false }]]); // Error, no x module M { export var [a, b] = [1, 2]; } function f15() { var a = "hello"; var b = 1; var c = true; return { a, b, c }; } function f16() { var { a, b, c } = f15(); } function f17({ a = "", b = 0, c = false }) { } f17({}); f17({ a: "hello" }); f17({ c: true }); f17(f15()); function f18() { var a: number; var b: string; var aa: number[]; ({ a, b } = { a, b }); ({ a, b } = { b, a }); [aa[0], b] = [a, b]; [a, b] = [b, a]; // Error [a = 1, b = "abc"] = [2, "def"]; } function f19() { var a, b; [a, b] = [1, 2]; [a, b] = [b, a]; ({ a, b } = { b, a }); [[a, b] = [1, 2]] = [[2, 3]]; var x = ([a, b] = [1, 2]); } function f20(v: [number, number, number]) { var x: number; var y: number; var z: number; var a0: []; var a1: [number]; var a2: [number, number]; var a3: [number, number, number]; var [...a3] = v; var [x, ...a2] = v; var [x, y, ...a1] = v; var [x, y, z, ...a0] = v; [...a3] = v; [x, ...a2] = v; [x, y, ...a1] = v; [x, y, z, ...a0] = v; } function f21(v: [number, string, boolean]) { var x: number; var y: string; var z: boolean; var a0: [number, string, boolean]; var a1: [string, boolean]; var a2: [boolean]; var a3: []; var [...a0] = v; var [x, ...a1] = v; var [x, y, ...a2] = v; var [x, y, z, ...a3] = v; [...a0] = v; [x, ...a1] = v; [x, y, ...a2] = v; [x, y, z, ...a3] = v; }
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/declaredClassMergedwithSelf.ts
TypeScript
// @Filename: file1.ts declare class C1 {} declare class C1 {} declare class C2 {} interface C2 {} declare class C2 {} // @Filename: file2.ts declare class C3 { } // @Filename: file3.ts declare class C3 { }
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/decoratedClassExportsCommonJS1.ts
TypeScript
// @target: es6 // @experimentaldecorators: true // @emitDecoratorMetadata: true // @module: commonjs // @filename: a.ts declare function forwardRef(x: any): any; declare var Something: any; @Something({ v: () => Testing123 }) export class Testing123 { static prop0: string; static prop1 = Testing123.prop0; }
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/decoratedClassExportsCommonJS2.ts
TypeScript
// @target: es6 // @experimentaldecorators: true // @emitDecoratorMetadata: true // @module: commonjs // @filename: a.ts declare function forwardRef(x: any): any; declare var Something: any; @Something({ v: () => Testing123 }) export class Testing123 { }
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/decoratedClassExportsSystem1.ts
TypeScript
// @target: es6 // @experimentaldecorators: true // @emitDecoratorMetadata: true // @module: system // @filename: a.ts declare function forwardRef(x: any): any; declare var Something: any; @Something({ v: () => Testing123 }) export class Testing123 { static prop0: string; static prop1 = Testing123.prop0; }
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/decoratedClassExportsSystem2.ts
TypeScript
// @target: es6 // @experimentaldecorators: true // @emitDecoratorMetadata: true // @module: system // @filename: a.ts declare function forwardRef(x: any): any; declare var Something: any; @Something({ v: () => Testing123 }) export class Testing123 { }
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/decoratorMetadata.ts
TypeScript
// @experimentalDecorators: true // @emitDecoratorMetadata: true // @target: es5 // @module: commonjs // @filename: service.ts export default class Service { } // @filename: component.ts import Service from "./service"; declare var decorator: any; @decorator class MyComponent { constructor(public Service: Service) { } @decorator method(x: this) { } }
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/decoratorMetadataWithTypeOnlyImport.ts
TypeScript
// @experimentalDecorators: true // @emitDecoratorMetadata: true // @target: es5 // @module: commonjs // @filename: service.ts export class Service { } // @filename: component.ts import type { Service } from "./service"; declare var decorator: any; @decorator class MyComponent { constructor(public Service: Service) { } @decorator method(x: this) { } }
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/decoratorMetadataWithTypeOnlyImport2.ts
TypeScript
// @experimentalDecorators: true // @emitDecoratorMetadata: true // @filename: services.ts export namespace Services { export class Service {} } // @filename: index.ts import type { Services } from './services'; declare const decorator: any; export class Main { @decorator() field: Services.Service; }
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/decoratorOnClass9.ts
TypeScript
// @target:es5 // @experimentaldecorators: true declare var dec: any; class A {} // https://github.com/Microsoft/TypeScript/issues/16417 @dec class B extends A { static x = 1; static y = B.x; m() { return B.x; } }
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/decoratorOnClassConstructor4.ts
TypeScript
// @target: es5 // @module: commonjs // @experimentaldecorators: true // @emitdecoratormetadata: true declare var dec: any; @dec class A { } @dec class B { constructor(x: number) {} } @dec class C extends A { }
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/decoratorOnClassMethod11.ts
TypeScript
// @target: ES5 // @experimentaldecorators: true module M { class C { decorator(target: Object, key: string): void { } @(this.decorator) method() { } } }
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/decoratorOnClassMethod12.ts
TypeScript
// @target: ES5 // @experimentaldecorators: true module M { class S { decorator(target: Object, key: string): void { } } class C extends S { @(super.decorator) method() { } } }
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/decoratorOnClassMethod14.ts
TypeScript
// @target: esnext // @experimentaldecorators: true // @emitdecoratormetadata: true declare var decorator: any; class Foo { private prop = () => { return 0; } @decorator foo() { return 0; } }
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/decoratorOnClassMethod15.ts
TypeScript
// @target: esnext // @experimentaldecorators: true // @emitdecoratormetadata: true declare var decorator: any; class Foo { private prop = 1 @decorator foo() { return 0; } }
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/decoratorOnClassMethod16.ts
TypeScript
// @target: esnext // @experimentaldecorators: true // @emitdecoratormetadata: true declare var decorator: any; class Foo { private prop @decorator foo() { return 0; } }
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/decoratorOnClassMethod18.ts
TypeScript
// @target: esnext // @experimentaldecorators: true // @emitdecoratormetadata: true declare var decorator: any; class Foo { p1 @decorator() p2; }
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/decrementOperatorWithAnyOtherType.ts
TypeScript
// -- operator on any type var ANY: any; var ANY1: any; var ANY2: any[] = ["", ""]; var obj = {x:1,y:null}; class A { public a: any; } module M { export var n: any; } var objA = new A(); // any type var var ResultIsNumber1 = --ANY; var ResultIsNumber2 = --ANY1; var ResultIsNumber3 = ANY1--; var ResultIsNumber4 = ANY1--; // expressions var ResultIsNumber5 = --ANY2[0]; var ResultIsNumber6 = --obj.x; var ResultIsNumber7 = --obj.y; var ResultIsNumber8 = --objA.a; var ResultIsNumber = --M.n; var ResultIsNumber9 = ANY2[0]--; var ResultIsNumber10 = obj.x--; var ResultIsNumber11 = obj.y--; var ResultIsNumber12 = objA.a--; var ResultIsNumber13 = M.n--; // miss assignment opertors --ANY; --ANY1; --ANY2[0]; --ANY, --ANY1; --objA.a; --M.n; ANY--; ANY1--; ANY2[0]--; ANY--, ANY1--; objA.a--; M.n--;
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/decrementOperatorWithEnumType.ts
TypeScript
// -- operator on enum type enum ENUM1 { A, B, "" }; // expression var ResultIsNumber1 = --ENUM1["A"]; var ResultIsNumber2 = ENUM1.A--; // miss assignment operator --ENUM1["A"]; ENUM1[A]--;
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/decrementOperatorWithEnumTypeInvalidOperations.ts
TypeScript
// -- operator on enum type enum ENUM { }; enum ENUM1 { A, B, "" }; // enum type var var ResultIsNumber1 = --ENUM; var ResultIsNumber2 = --ENUM1; var ResultIsNumber3 = ENUM--; var ResultIsNumber4 = ENUM1--; // enum type expressions var ResultIsNumber5 = --(ENUM["A"] + ENUM.B); var ResultIsNumber6 = (ENUM.A + ENUM["B"])--; // miss assignment operator --ENUM; --ENUM1; ENUM--; ENUM1--;
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/decrementOperatorWithNumberType.ts
TypeScript
// -- operator on number type var NUMBER: number; var NUMBER1: number[] = [1, 2]; class A { public a: number; } module M { export var n: number; } var objA = new A(); // number type var var ResultIsNumber1 = --NUMBER; var ResultIsNumber2 = NUMBER--; // expressions var ResultIsNumber3 = --objA.a; var ResultIsNumber4 = --M.n; var ResultIsNumber5 = objA.a--; var ResultIsNumber6 = M.n--; var ResultIsNumber7 = NUMBER1[0]--; // miss assignment operators --NUMBER; --NUMBER1[0]; --objA.a; --M.n; --objA.a, M.n; NUMBER--; NUMBER1[0]--; objA.a--; M.n--; objA.a--, M.n--;
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/decrementOperatorWithNumberTypeInvalidOperations.ts
TypeScript
// -- operator on number type var NUMBER: number; var NUMBER1: number[] = [1, 2]; function foo(): number { return 1; } class A { public a: number; static foo() { return 1; } } module M { export var n: number; } var objA = new A(); //number type var var ResultIsNumber1 = --NUMBER1; var ResultIsNumber2 = NUMBER1--; // number type literal var ResultIsNumber3 = --1; var ResultIsNumber4 = --{ x: 1, y: 2}; var ResultIsNumber5 = --{ x: 1, y: (n: number) => { return n; } }; var ResultIsNumber6 = 1--; var ResultIsNumber7 = { x: 1, y: 2 }--; var ResultIsNumber8 = { x: 1, y: (n: number) => { return n; } }--; // number type expressions var ResultIsNumber9 = --foo(); var ResultIsNumber10 = --A.foo(); var ResultIsNumber11 = --(NUMBER + NUMBER); var ResultIsNumber12 = foo()--; var ResultIsNumber13 = A.foo()--; var ResultIsNumber14 = (NUMBER + NUMBER)--; // miss assignment operator --1; --NUMBER1; --foo(); 1--; NUMBER1--; foo()--;
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University
crates/swc_ecma_parser/tests/tsc/decrementOperatorWithUnsupportedBooleanType.ts
TypeScript
// -- operator on boolean type var BOOLEAN: boolean; function foo(): boolean { return true; } class A { public a: boolean; static foo() { return true; } } module M { export var n: boolean; } var objA = new A(); // boolean type var var ResultIsNumber1 = --BOOLEAN; var ResultIsNumber2 = BOOLEAN--; // boolean type literal var ResultIsNumber3 = --true; var ResultIsNumber4 = --{ x: true, y: false }; var ResultIsNumber5 = --{ x: true, y: (n: boolean) => { return n; } }; var ResultIsNumber6 = true--; var ResultIsNumber7 = { x: true, y: false }--; var ResultIsNumber8 = { x: true, y: (n: boolean) => { return n; } }--; // boolean type expressions var ResultIsNumber9 = --objA.a; var ResultIsNumber10 = --M.n; var ResultIsNumber11 = --foo(); var ResultIsNumber12 = --A.foo(); var ResultIsNumber13 = foo()--; var ResultIsNumber14 = A.foo()--; var ResultIsNumber15 = objA.a--; var ResultIsNumber16 = M.n--; // miss assignment operators --true; --BOOLEAN; --foo(); --objA.a; --M.n; --objA.a, M.n; true--; BOOLEAN--; foo()--; objA.a--; M.n--; objA.a--, M.n--;
willcrichton/ilc-swc
1
Rust
willcrichton
Will Crichton
Brown University