Skip to content

Commit 2bc9115

Browse files
wjhsfnolanlawson
andauthored
feat(types): update types for v7 (#4186)
Co-authored-by: Nolan Lawson <nlawson@salesforce.com>
1 parent c0423f5 commit 2bc9115

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+1112
-481
lines changed

.eslintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@
176176
},
177177
{
178178
// Not covered by any tsconfig, so typed rules won't work, but we don't need them anyway
179-
"files": ["jest.config.js"],
179+
"files": ["jest.config.js", "**/rollup.config.js"],
180180
"extends": ["plugin:@typescript-eslint/disable-type-checked"]
181181
},
182182
{

.github/workflows/unit.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ jobs:
6262
run: yarn lint
6363
- name: Check the size of the LWC bundle
6464
run: yarn bundlesize
65+
- name: Check types
66+
run: yarn test:types
6567
- name: Run Jest tests
6668
run: yarn test:ci
6769
- name: Upload Jest coverage report

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
-local-*
33
.DS_Store
44
dist/
5-
types/
65
node_modules/
76
coverage/
87
.idea/

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"test:performance": "nx test @lwc/perf-benchmarks",
2727
"test:performance:best": "nx test:best @lwc/perf-benchmarks",
2828
"test:performance:best:ci": "nx test:best:ci @lwc/perf-benchmarks",
29+
"test:types": "nx test @lwc/integration-types",
2930
"release:version": "./scripts/release/version.js"
3031
},
3132
"//": {

packages/@lwc/engine-core/src/framework/base-lightning-element.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, salesforce.com, inc.
2+
* Copyright (c) 2024, Salesforce, Inc.
33
* All rights reserved.
44
* SPDX-License-Identifier: MIT
55
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
@@ -56,7 +56,7 @@ import { getVMBeingRendered, isUpdatingTemplate, Template } from './template';
5656
import { HTMLElementConstructor } from './base-bridge-element';
5757
import { updateComponentValue } from './update-component-value';
5858
import { markLockerLiveObject } from './membrane';
59-
import { TemplateStylesheetFactories } from './stylesheet';
59+
import { Stylesheets } from './stylesheet';
6060
import { instrumentInstance } from './runtime-instrumentation';
6161
import { applyShadowMigrateMode } from './shadow-migration-mode';
6262

@@ -143,7 +143,7 @@ export interface LightningElementConstructor {
143143
renderMode?: 'light' | 'shadow';
144144
formAssociated?: boolean;
145145
shadowSupportMode?: ShadowSupportMode;
146-
stylesheets: TemplateStylesheetFactories;
146+
stylesheets: Stylesheets;
147147
}
148148

149149
type HTMLElementTheGoodParts = { toString: () => string } & Pick<
@@ -192,9 +192,17 @@ type RefNodes = { [name: string]: Element };
192192

193193
const refsCache: WeakMap<RefVNodes, RefNodes> = new WeakMap();
194194

195+
/**
196+
* A `LightningElement` will always be attached to an [`HTMLElement`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement),
197+
* rather than the more broad `Element` used by the generic shadow root interface.
198+
*/
199+
export interface LightningElementShadowRoot extends ShadowRoot {
200+
readonly host: HTMLElement;
201+
}
202+
195203
export interface LightningElement extends HTMLElementTheGoodParts, AccessibleElementProperties {
196204
constructor: LightningElementConstructor;
197-
template: ShadowRoot | null;
205+
template: LightningElementShadowRoot | null;
198206
refs: RefNodes | undefined;
199207
hostElement: Element;
200208
render(): Template;
@@ -273,7 +281,7 @@ export const LightningElement: LightningElementConstructor = function (
273281
return this;
274282
};
275283

276-
function doAttachShadow(vm: VM): ShadowRoot {
284+
function doAttachShadow(vm: VM): LightningElementShadowRoot {
277285
const {
278286
elm,
279287
mode,
@@ -531,7 +539,7 @@ function warnIfInvokedDuringConstruction(vm: VM, methodOrPropName: string) {
531539
return getClassList(elm);
532540
},
533541

534-
get template(): ShadowRoot | null {
542+
get template(): LightningElementShadowRoot | null {
535543
const vm = getAssociatedVM(this);
536544

537545
if (process.env.NODE_ENV !== 'production') {

packages/@lwc/engine-core/src/framework/check-version-mismatch.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { isNull, LWC_VERSION, LWC_VERSION_COMMENT_REGEX } from '@lwc/shared';
99
import { logError } from '../shared/logger';
1010

1111
import { Template } from './template';
12-
import { StylesheetFactory } from './stylesheet';
12+
import { Stylesheet } from './stylesheet';
1313
import { LightningElementConstructor } from './base-lightning-element';
1414
import { report, ReportingEventId } from './reporting';
1515

@@ -30,10 +30,10 @@ if (process.env.NODE_ENV === 'test-karma-lwc') {
3030
* @param type
3131
*/
3232
export function checkVersionMismatch(func: Template, type: 'template'): void;
33-
export function checkVersionMismatch(func: StylesheetFactory, type: 'stylesheet'): void;
33+
export function checkVersionMismatch(func: Stylesheet, type: 'stylesheet'): void;
3434
export function checkVersionMismatch(func: LightningElementConstructor, type: 'component'): void;
3535
export function checkVersionMismatch(
36-
func: Template | StylesheetFactory | LightningElementConstructor,
36+
func: Template | Stylesheet | LightningElementConstructor,
3737
type: 'template' | 'stylesheet' | 'component'
3838
) {
3939
const versionMatcher = func.toString().match(LWC_VERSION_COMMENT_REGEX);

packages/@lwc/engine-core/src/framework/decorators/api.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, salesforce.com, inc.
2+
* Copyright (c) 2024, Salesforce, Inc.
33
* All rights reserved.
44
* SPDX-License-Identifier: MIT
55
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
@@ -13,15 +13,16 @@ import { getAssociatedVM } from '../vm';
1313
import { isUpdatingTemplate, getVMBeingRendered } from '../template';
1414

1515
/**
16-
* The @api decorator marks public fields and public methods in
16+
* The `@api` decorator marks public fields and public methods in
1717
* LWC Components. This function implements the internals of this
1818
* decorator.
19-
* @param target
20-
* @param propertyKey
21-
* @param descriptor
2219
*/
23-
export default function api(target: any, propertyKey: string, descriptor: PropertyDescriptor): void;
24-
export default function api() {
20+
export default function api(
21+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
22+
value: unknown,
23+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
24+
context: ClassMemberDecoratorContext | string | symbol
25+
): void {
2526
if (process.env.NODE_ENV !== 'production') {
2627
assert.fail(`@api decorator can only be used as a decorator function.`);
2728
}

packages/@lwc/engine-core/src/framework/decorators/register.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,19 +59,13 @@ interface RegisterDecoratorMeta {
5959
readonly fields?: string[];
6060
}
6161

62-
const enum DescriptorType {
63-
Method = 'method',
64-
Accessor = 'accessor',
65-
Field = 'field',
66-
}
67-
68-
function getClassDescriptorType(descriptor: PropertyDescriptor): DescriptorType {
62+
function getClassDescriptorType(descriptor: PropertyDescriptor): string {
6963
if (isFunction(descriptor.value)) {
70-
return DescriptorType.Method;
64+
return 'method';
7165
} else if (isFunction(descriptor.set) || isFunction(descriptor.get)) {
72-
return DescriptorType.Accessor;
66+
return 'accessor';
7367
} else {
74-
return DescriptorType.Field;
68+
return 'field';
7569
}
7670
}
7771

packages/@lwc/engine-core/src/framework/decorators/track.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, salesforce.com, inc.
2+
* Copyright (c) 2024, Salesforce, Inc.
33
* All rights reserved.
44
* SPDX-License-Identifier: MIT
55
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
@@ -15,19 +15,15 @@ import { updateComponentValue } from '../update-component-value';
1515
import { logError } from '../../shared/logger';
1616

1717
/**
18-
* The @track decorator function marks field values as reactive in
18+
* The `@track` decorator function marks field values as reactive in
1919
* LWC Components. This function can also be invoked directly
2020
* with any value to obtain the trackable version of the value.
21-
* @param target
22-
* @param propertyKey
23-
* @param descriptor
2421
*/
2522
export default function track(
26-
target: any,
27-
propertyKey: string,
28-
descriptor: PropertyDescriptor
29-
): any;
30-
export default function track(target: any): any {
23+
value: unknown,
24+
context: ClassMemberDecoratorContext | string | symbol
25+
): void;
26+
export default function track<T>(target: T): T {
3127
if (arguments.length === 1) {
3228
return getReactiveProxy(target);
3329
}

packages/@lwc/engine-core/src/framework/decorators/wire.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, salesforce.com, inc.
2+
* Copyright (c) 2024, Salesforce, Inc.
33
* All rights reserved.
44
* SPDX-License-Identifier: MIT
55
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
@@ -12,16 +12,22 @@ import { WireAdapterConstructor } from '../wiring';
1212
import { updateComponentValue } from '../update-component-value';
1313

1414
/**
15-
* The @wire decorator wires fields and methods to a wire adapter in
16-
* LWC Components. This function implements the internals of this
17-
* decorator.
18-
* @param _adapter
19-
* @param _config
15+
* Decorator factory to wire a property or method to a wire adapter data source.
16+
* @param adapter the adapter used to provision data
17+
* @param config configuration object for the adapter
18+
* @returns A decorator function
19+
* @example
20+
* export default class WireExample extends LightningElement {
21+
* \@api bookId;
22+
* \@wire(getBook, { id: '$bookId'}) book;
23+
* }
2024
*/
2125
export default function wire(
22-
_adapter: WireAdapterConstructor,
23-
_config?: Record<string, any>
24-
): PropertyDecorator | MethodDecorator {
26+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
27+
adapter: WireAdapterConstructor,
28+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
29+
config?: Record<string, any>
30+
): (value: unknown, context: ClassMemberDecoratorContext | string | symbol) => void {
2531
if (process.env.NODE_ENV !== 'production') {
2632
assert.fail('@wire(adapter, config?) may only be used as a decorator.');
2733
}

packages/@lwc/engine-core/src/framework/def.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -123,17 +123,17 @@ function createComponentDef(Ctor: LightningElementConstructor): ComponentDef {
123123

124124
if (
125125
!isUndefined(ctorShadowSupportMode) &&
126-
ctorShadowSupportMode !== ShadowSupportMode.Any &&
127-
ctorShadowSupportMode !== ShadowSupportMode.Default &&
128-
ctorShadowSupportMode !== ShadowSupportMode.Native
126+
ctorShadowSupportMode !== 'any' &&
127+
ctorShadowSupportMode !== 'reset' &&
128+
ctorShadowSupportMode !== 'native'
129129
) {
130130
logError(
131131
`Invalid value for static property shadowSupportMode: '${ctorShadowSupportMode}'`
132132
);
133133
}
134134

135135
// TODO [#3971]: Completely remove shadowSupportMode "any"
136-
if (ctorShadowSupportMode === ShadowSupportMode.Any) {
136+
if (ctorShadowSupportMode === 'any') {
137137
logWarn(
138138
`Invalid value 'any' for static property shadowSupportMode. 'any' is deprecated and will be removed in a future release--use 'native' instead.`
139139
);
@@ -202,8 +202,7 @@ function createComponentDef(Ctor: LightningElementConstructor): ComponentDef {
202202

203203
if (
204204
isReportingEnabled() &&
205-
(shadowSupportMode === ShadowSupportMode.Any ||
206-
shadowSupportMode === ShadowSupportMode.Native)
205+
(shadowSupportMode === 'any' || shadowSupportMode === 'native')
207206
) {
208207
report(ReportingEventId.ShadowSupportModeUsage, {
209208
tagName: Ctor.name,
@@ -342,21 +341,17 @@ const lightingElementDef: ComponentDef = {
342341
propsConfig: EmptyObject,
343342
methods: EmptyObject,
344343
renderMode: RenderMode.Shadow,
345-
shadowSupportMode: ShadowSupportMode.Default,
344+
shadowSupportMode: 'reset',
346345
formAssociated: undefined,
347346
wire: EmptyObject,
348347
bridge: BaseBridgeElement,
349348
template: defaultEmptyTemplate,
350349
render: LightningElement.prototype.render,
351350
};
352351

353-
const enum PropDefType {
354-
any = 'any',
355-
}
356-
357352
interface PropDef {
358353
config: number;
359-
type: PropDefType;
354+
type: 'any';
360355
attr: string;
361356
}
362357
type PublicMethod = (...args: any[]) => any;
@@ -384,7 +379,7 @@ export function getComponentDef(Ctor: any): PublicComponentDef {
384379
// avoid leaking the reference to the public props descriptors
385380
publicProps[key] = {
386381
config: propsConfig[key] || 0, // a property by default
387-
type: PropDefType.any, // no type inference for public services
382+
type: 'any', // no type inference for public services
388383
attr: htmlPropertyToAttribute(key),
389384
};
390385
}

packages/@lwc/engine-core/src/framework/freeze-template.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
} from '@lwc/shared';
2323
import { logWarnOnce } from '../shared/logger';
2424
import { Template } from './template';
25-
import { StylesheetFactory, TemplateStylesheetFactories } from './stylesheet';
25+
import { Stylesheet, Stylesheets } from './stylesheet';
2626
import { onReportingEnabled, report, ReportingEventId } from './reporting';
2727

2828
// See @lwc/engine-core/src/framework/template.ts
@@ -111,7 +111,7 @@ function reportStylesheetViolation(prop: StylesheetProp) {
111111

112112
// Warn if the user tries to mutate a stylesheets array, e.g.:
113113
// `tmpl.stylesheets.push(someStylesheetFunction)`
114-
function warnOnArrayMutation(stylesheets: TemplateStylesheetFactories) {
114+
function warnOnArrayMutation(stylesheets: Stylesheets) {
115115
// We can't handle users calling Array.prototype.slice.call(tmpl.stylesheets), but
116116
// we can at least warn when they use the most common mutation methods.
117117
for (const prop of ARRAY_MUTATION_METHODS) {
@@ -126,7 +126,7 @@ function warnOnArrayMutation(stylesheets: TemplateStylesheetFactories) {
126126

127127
// Warn if the user tries to mutate a stylesheet factory function, e.g.:
128128
// `stylesheet.$scoped$ = true`
129-
function warnOnStylesheetFunctionMutation(stylesheet: StylesheetFactory) {
129+
function warnOnStylesheetFunctionMutation(stylesheet: Stylesheet) {
130130
for (const prop of STYLESHEET_PROPS) {
131131
let value = (stylesheet as any)[prop];
132132
defineProperty(stylesheet, prop, {
@@ -144,7 +144,7 @@ function warnOnStylesheetFunctionMutation(stylesheet: StylesheetFactory) {
144144
}
145145

146146
// Warn on either array or stylesheet (function) mutation, in a deeply-nested array
147-
function trackStylesheetsMutation(stylesheets: TemplateStylesheetFactories) {
147+
function trackStylesheetsMutation(stylesheets: Stylesheets) {
148148
traverseStylesheets(stylesheets, (subStylesheets) => {
149149
if (isArray(subStylesheets)) {
150150
warnOnArrayMutation(subStylesheets);
@@ -155,16 +155,16 @@ function trackStylesheetsMutation(stylesheets: TemplateStylesheetFactories) {
155155
}
156156

157157
// Deeply freeze the entire array (of arrays) of stylesheet factory functions
158-
function deepFreeze(stylesheets: TemplateStylesheetFactories) {
158+
function deepFreeze(stylesheets: Stylesheets) {
159159
traverseStylesheets(stylesheets, (subStylesheets) => {
160160
freeze(subStylesheets);
161161
});
162162
}
163163

164164
// Deep-traverse an array (of arrays) of stylesheet factory functions, and call the callback for every array/function
165165
function traverseStylesheets(
166-
stylesheets: TemplateStylesheetFactories,
167-
callback: (subStylesheets: TemplateStylesheetFactories | StylesheetFactory) => void
166+
stylesheets: Stylesheets,
167+
callback: (subStylesheets: Stylesheets | Stylesheet) => void
168168
) {
169169
callback(stylesheets);
170170
for (let i = 0; i < stylesheets.length; i++) {

0 commit comments

Comments
 (0)